This shell script is useful for backing up important files and storing them remotely in an encrypted archive. Many ISPs give their customers some free webspace on a server for personal web pages, if you're not using yours then why not turn it into an offsite backup location?
#!/bin/sh
### Change all these
BACKUP_LIST=~rob/backup.list
EXCLUDE_FILE=~rob/backup.exclude
OUTPUT_DIR=~rob
OUTPUT_FILE=backup.tar
CRYPT_KEY="y0uR cRypT!KeY in H3re"
FTP_USER=rob@ftpbox
FTP_PASS=secret_password
FTP_SERVER=files.myisp.com
FTP_DIR=backupdir
### Dont change these
FTP_OK_MSG="^226 "
FTP_LOG=$0.ftp.log
OUTPUT_ZIPFILE=$OUTPUT_FILE.gz
OUTPUT_ENCRYPTED=$OUTPUT_ZIPFILE.gpg
doBackup(){
SOURCE=$1
if [ ! -e $SOURCE ]; then
echo "$0 WARNING file $SOURCE could not be found" 1>&2
else
echo backing up $SOURCE to $OUTPUT_FILE
tar -rPf $OUTPUT_FILE --exclude-from=$EXCLUDE_FILE $SOURCE
fi
}
reportFileSize(){
FILE=$1
MSG=$2
echo $MSG $FILE is `ls -l $FILE | cut -f5 -d' '` bytes
}
###################################
# Prepare everything...
###################################
cd $OUTPUT_DIR
rm -f $OUTPUT_FILE
rm -f $OUTPUT_ZIPFILE
rm -f $OUTPUT_ENCRYPTED
rm -f $FTP_LOG
if [ ! -e $BACKUP_LIST ]; then
echo "$0 could not find the backup list $BACKUP_LIST" 1>&2
exit 1
fi
if [ ! -e $EXCLUDE_FILE ]; then
# We need the file to exist otherwise the tar command fails
touch $EXCLUDE_FILE
fi
###################################
# Backup the files into an archive and compress it
###################################
echo Running backup with the following excludes...
cat $EXCLUDE_FILE
# Create the archive and put a copy of the backup list into it
tar -cPf $OUTPUT_FILE $BACKUP_LIST
# Read the entries from the BACKUP_LIST file, and add each one into the archive
while read ENTRY
do
doBackup $ENTRY
done < $BACKUP_LIST
reportFileSize $OUTPUT_FILE "Before compression"
# Compress the archive
gzip $OUTPUT_FILE
reportFileSize $OUTPUT_ZIPFILE "After compression"
###################################
# Encrypt backup file
###################################
gpg -c --passphrase "$CRYPT_KEY" $OUTPUT_ZIPFILE
reportFileSize $OUTPUT_ENCRYPTED "After encryption"
###################################
# FTP backup file
###################################
ftp -nv $FTP_SERVER > $FTP_LOG << EOF
user $FTP_USER $FTP_PASS
cd $FTP_DIR
put $OUTPUT_ENCRYPTED
bye
EOF
OK_MSG_COUNT=`grep -c "$FTP_OK_MSG" $FTP_LOG`
if [ $OK_MSG_COUNT = 1 ]; then
echo FTP transfer completed ok
EXIT_CODE=0
else
echo FTP transfer failed! 1>&2
cat $FTP_LOG 1>&2
EXIT_CODE=1
fi
###################################
# Clean up and exit (leave the zipped backup file in place)
###################################
rm -f $OUTPUT_FILE
rm -f $OUTPUT_ENCRYPTED
rm -f $FTP_LOG
exit $EXIT_CODE
To use the script you will need to change the 9 values indicated, as follows:
/etc /var/www /home/rob/plans/world_domination.txt /home/rob/docs
/var/www containing all your websites, but you don't want
to back up all the logs which are stored in /var/www/logs. By including an entry for /var/www in your BACKUP_LIST
file, and including /var/www/logs in your EXCLUDE_FILE file, you will be backing up everything in that directory except for
the logs. This is simpler than individually naming each of the sub-directories of /var/www that you want to include, and is
also more future-proof - if you add a new website next month it will get backed up without you having to remember to add it into your list.
'.tar'. Note that
once the backup has been compressed and encrypted the name will have .gz.gpg appended to it, so if you specify an OUTPUT_FILE of
backup.tar, the file that gets copied to the FTP server will actually be called backup.tar.gz.gpg
Depending on your system, you may also need to install the gpg utility to perform the encryption, you can get it from
here.
To decrypt the backup file, just use the gpg utility against the encrypted archive like this (entering your key when prompted to do so):
gpg backup.tar.gz.gpg
It should be obvious that this script is NOT very secure, it contains both the crypto-key for your backup, and the password for your
FTP account in plaintext. As a minimum you should change the permissions on the script file so that only you have read- and execute-access to it. Also
bear in mind that because the crypto-key is passed to gpg as a command-line parameter, the key will be visible in the process list of your system
(accessible via ps -ef) while the encryption command is running.