Quick and Easy Automatic Backup Script

At Rimuhosting we do a weekly snapshot backup of your VPS. This means that if anything goes wrong you can revert to the previous image. However since its weekly, not daily, you may find you have lost the data between those dates.
This is a backup script that will backup your database and required files to our FTP every day, and keep a 7 day copy to solve that problem.
If you need the script to do more days then just change the variable
DAYOFWEEK=`date +%a`
To something like
DAYOFWEEK=`date +%d`

This is the day of the month so will make it run a 30/31 day backup cycle. However be aware this will use a lot more disk space on backupspace.rimuhosting.com
Make sure you have lftp installed before you run this.

Add this into a file in say /root/backup.sh

# Key things to remember, no spaces in pathnames, and try to use fill paths (beginning with / )
# now fill in these few variables for me
# change to use
# FTP username and Pass
# this is a list of directories you want backed up. No trailing / needed.
INCLUDES="/home /var/www"
# I added a mysql user called backup with permissions to SELECT and LOCKING only for this backup
# CREATE USER backup@'localhost' IDENTIFIED BY 'backuppassword';
# change this variable to anything but 1 to disable mysql backups ( some prefer to backup the binlog )
# this stuff is probably not needing to be changed
DAYOFWEEK=`date +%a`
DATESTAMP=`date +%d%m%y`
cd /
# remove all older files
rm -rf ${TMPDIR}/*
# create directory structure
/bin/mkdir -p ${TMPDIR}/files &&
/bin/mkdir -p ${TMPDIR}/db &&
/bin/mkdir -p ${TMPDIR}/archives &&
if [ $MYSQLBACKUP = 1 ];then
/usr/bin/mysqldump -u${DBUSER} -p${DBPASS} -A | gzip -c > ${TMPDIR}/db/mysql_backup-${DATESTAMP}-${DAYOFWEEK}.sql.gz
for ITEMI in ${INCLUDES} ; do
/bin/mkdir -p ${TMPDIR}/files/${ITEMI}/
/usr/bin/rsync -aq ${ITEMI}/* ${TMPDIR}/files/${ITEMI}/
/bin/tar jcf ${TMPDIR}/archives/file-backup-${DATESTAMP}-${DAYOFWEEK}.tar.bz2 ${TMPDIR}/files/ > /dev/null 2>&1 &&
/usr/bin/lftp -u "${FTPUSER},${FTPPASS}" backupspace.rimuhosting.com -e "set ftp:ssl-protect-data true; mrm *-${DAYOFWEEK}.* ; put ${TMPDIR}/archives/file-backup-${DATESTAMP}-${DAYOFWEEK}.tar.bz2; put ${TMPDIR}/db/mysql_backup-${DATESTAMP}-${DAYOFWEEK}.sql ; exit" >/dev/null
# remove all older files
#rm -rf ${TMPDIR}/*

chmod +x /root/backup.sh

Then add this into /etc/cron.d/backup

0 2 * * * root /root/backup.sh

This will run it at 2am every morning.

Its important to note, files put in /etc/cron.daily/ do not need/want a suffix (ie .sh )

If you have any problems at all, let us know, just pop in a support ticket and we can get it going for you.

If you want larger incremental backups see this blog post http://blog.rimuhosting.com/2010/01/14/rdiff-backup-script-using-sshfs-for-larger-backups/

This entry was posted in Featured, Rimuhosting and tagged , , , , , , , , . Bookmark the permalink.

Comments are closed.