whird.jpope.org cover image

Script for mysql backups

Here is my quick mysql backup script that I have used for a couple of years. Posting it here for reference sake... #!/bin/bash ## if run daily (say, via cron) you will end up with a tar file that contains ## a mysqldump of each database on your server. ## the script will also delete backups that are older than two weeks ## ## jpope <jpope at jpope dot org> ## licensed under WTFPL http://www.wtfpl.net/ ## EDIT OPTIONS ## workingdir=/path/to/mysql/backups dte=$(date +%Y%m%d) dbuser=username dbpass=userpassword dbhost=localhost tmpfile=/tmp/databaselist ## END EDIT OPTIONS ## dblist=$(mysql -u$dbuser -p$dbpass -h$dbhost -e"select distinct t.TABLE_SCHEMA \ from information_schema.tables t WHERE t.TABLE_SCHEMA NOT IN ('information_schema','mysql','performance_schema');" \ | grep -v "^+" | sed "s,\t,.," | grep -v TABLE_SCHEMA > $tmpfile) cd $workingdir mkdir $dte cd $dte databases=$( cat $tmpfile | wc -l ) i=1 ## backup databases $dblist while [ $i -le $databases ] do db=$(cat $tmpfile | sed -…