I wrote some scripts to run from cron to keep my stuff backed up properly. I wrote one to backup my sql databases and another to use rsync to backup my home directory (which also contains sql backups, mail, etc).
This first script, dumps SQL databases and creates a tar.bz2 nightly of all dumped databases - purges dumps older than 7 days.
#!/bin/sh # Backup sql databases DBUSER="" DBPASS="" MYSQLDUMP="mysqldump -u $DBUSER -p$DBPASS --add-drop-database --add-drop-table" DATABASES="space separated list of databases" STAMP=`date +%Y%m%d` # Purge all database dumps more than 7 days old. find `dirname $0`/sql/ -type f -iname '*.tar.bz2' -mtime +7 | xargs rm &> /dev/null # Dump each db to a $db-date.sql file for db in $DATABASES; do FILENAME="$db-$STAMP.sql" $MYSQLDUMP $db > `dirname $0`/sql/$FILENAME if [ $? != 0 ]; then echo "Error dumping database: $db" rm -f `dirname $0`/sql/$FILENAME fi done # Create a .tar.bz2 of all db dumps (save on file count, and improve compression) cd `dirname $0`/sql rm -rf db-$STAMP.tar* tar cf db-$STAMP.tar *-$STAMP.sql bzip2 -9 db-$STAMP.tar rm -rf *.sql cd ..
The second script is an rsync script to backup my home directory (and can do other stuff too) to my server at home. This is currently not configured for automatic backups (need to set up an ssh priv/pub key for it yet).
#!/bin/sh # Automatically uses rsync to backup various directories PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin" SCRIPT_PATH=`dirname $0` REMOTE_ADDR="ryan@********:/home/ryan/backup/" BACKUP_DIRS="/home/ryan" RSYNC_ARGS=" -avz --progress --log-file=$SCRIPT_PATH/logs/rsync_$(date +%Y%m%d).log --exclude '$SCRIPT_PATH/logs/' --exclude '/home/ryan/.cpan/' " for dir in $BACKUP_DIRS do rsync $RSYNC_ARGS $dir $REMOTE_ADDR done
I just quickly pumped these scripts out, so they’re pretty rough and not well tested. That said, hopefully they will help someone out…
0 Response to “Automated Backup Scripts”