I recently moved some websites and I’m “re-setting up” my backups. I like to keep rolling daily and monthly backups, so I wrote a small bash script that looks like this:
!/bin/bash export BACKUP_ROOT=/root/www-backup if [ $1 = "daily" ]; then export BACKUP_SUBDIR1=daily export BACKUP_SUBDIR2=$(date +\%A) elif [ $1 = "monthly" ]; then export BACKUP_SUBDIR1=monthly export BACKUP_SUBDIR2=$(date +\%B) else export BACKUP_SUBDIR1=adhoc export BACKUP_SUBDIR2=$(date +\%F) fi export BACKUP_FULLPATH=$BACKUP_ROOT/$BACKUP_SUBDIR1/$BACKUP_SUBDIR2 echo "Ensuring backup path \"$BACKUP_FULLPATH\" exists" mkdir -p $BACKUP_FULLPATH echo "Updating httpd.conf backup file in /var/www/conf-backup/" /bin/rsync -u /etc/httpd/conf/httpd.conf /var/www/conf-backup/ echo "Updating files in $BACKUP_FULLPATH (from /var/www)" /bin/rsync -ax --delete /var/www/ $BACKUP_FULLPATH/
Then I have a bunch of database backup lines in my crontab that take the general form:
@daily mysqldump --password=dbPass --user=dbUser --host=dbHost --add-drop-table dbName > /var/www/dbName.sql
Finally, I have two other cronjobs that look like this to call the script:
daily /root/www-backup.sh daily @monthly /root/www-backup.sh monthly
I’ll do something else to occasionally backup the files to another server, but this gets things started.
Side note: During my experimentation (moving from an old server to a new one) I used a small C++ app I wrote to do help with the migration: https://github.com/juszak/LAMPbackup. Writing this in C++ probably seems a little odd (and it is). Bash or Python make more sense. However, I had some other reasons for doing this (mainly using this “lampbackup” project to experiment with a few C++ related things).