Encrypt and backup folder to email daily, when online 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=$(&lt; ./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 &gt; $DATE.enc

# Send email
MTO="mybackupemail@gmail.com"
MSUB="PW backup as on $DATE"
MATT="$DATE.enc"
EMAIL="backuprobot@mymachine.com" mutt -s "$MSUB" -a $MATT -- $MTO &lt; /dev/null

# Write date of backup to file
echo -n $DATE &gt; ./personalwiki/lastbackup
I would make the following changes, in order to make it more robust, and simpler:


Do not use openssl and symmetric encryption.  Use gpg and public key encryption.  The advantage is that you do not have to store any passwords, anywhere (keep the secret key away from the backup script).  Public key encryption is great for backups (you only need to access your secret key when you have to restore a backup, which happens rarely).
Do not mess around with timestamp files.  Instead, keep the last archive around, and inspect the datestamp of the archive (you can use find &lt;path&gt; -ctime 1 to see if the archive is older than 24 hours).  If your last backup is too old, make a new one.  Keeping one old backup around is another safety line for you, even though it's only stored locally.
Do not use a cron job.  Instead, invoke your script from a DHCP exit hook, which is run whenever your computer acquires an IP address.  You can then check if you have Internet access, and mail your backup straight away.  This also works if you're only online for a short while (and your cron job might miss that window of opportunity).
Check exit codes of everything and make sure your script alerts you somehow if things went wrong.  You might need to find a way to interface with your desktop environment in order to do that (notify-send works in Ubuntu).