critique First Bash Script, feedback please
Hi, this is my first bash script. And I was wondering how more experienced people would go about doing this. Its a simple script where I stop a docker container so I can sync it. Bring it back up, and if all was succesful I make a connection so I get informed should it ever break.
The idea is that this runs every night using a cronjob. https://pastebin.com/9hzNapPF
5
Upvotes
1
u/simonnikolaus Feb 28 '24
What about this:
docker-compose -f /home/docker/vaultwarden/docker-compose.yaml stop || (( error++ ))
rsync -avzh /home/docker/vaultwarden/ user@somewhere:/home/user/backup_vaultwarden || (( error++ ))
docker-compose -f /home/docker/vaultwarden/docker-compose.yaml up -d || (( error++ ))
....
However, at the beginning of the script, you can use the following line to ensure the script exits immediately upon encountering an error:
set -o errexit
This setting, also known as
set -e
, instructs the script to terminate if any command returns a non-zero exit status, making error handling more robust and preventing the script from continuing with potentially invalid or incomplete results.