I have a backup script which should backup a folder and send it to email. This should be done once a day. As this is on my laptop which is not online 24/7 I need to check that I am online and can send email. For this script I have an entry in crontab running every 2 hours.
Because the folder is a really important to me and I am quite new to BASH, I would like to ask you if there are any weak points in my code:
#!/usr/bin/env bash cd ~/projects DATE=`date +%Y%m%d` PASS="mysecretpasswordiwontwritehere" # lastbackup file stores date of last backup in YYYYMMDD format LASTBACKUP=$(< ./personalwiki/lastbackup) # Check if backup already exists for current date # and exit if yes. if [[ "$LASTBACKUP" == "$DATE" ]]; then echo "Do nothing, already backed up." exit 0 fi # Check if we are online # and exit if not. ping -c 1 www.google.com if [[ $? -ne 0 ]]; then echo "Offline, cannot do anything." exit 0 fi echo "Online and not equal, need to start backup" # Pack an encrypt folder tar cz personalwiki | openssl enc -aes-256-cbc -e -pass pass:$PASS > $DATE.enc # Send email MTO="firstname.lastname@example.org" MSUB="PW backup as on $DATE" MATT="$DATE.enc" EMAIL="email@example.com" mutt -s "$MSUB" -a $MATT -- $MTO < /dev/null # Write date of backup to file echo -n $DATE > ./personalwiki/lastbackup