Pete Freitag Pete Freitag

My MySQL Backup Script

Published on October 12, 2005
By Pete Freitag

Here's a simple mysql backup script that uses mysqldump, and keeps the previous two backups in gzipped tar files.

#!/bin/bash

#makes a backup of all databases in the db list
dblist="db1 db2 db3 etc"

backup_dir="/home/backup"

for db in $dblist
do
        mysqldump $db > $backup_dir/mysql/$db.sql
done

if [ -f $backup_dir/mysql.2.tar.gz ]
then 
mv $backup_dir/mysql.2.tar.gz $backup_dir/mysql.3.tar.gz
fi

if [ -f $backup_dir/mysql.tar.gz ]
then 
mv $backup_dir/mysql.tar.gz $backup_dir/mysql.2.tar.gz
fi

tar -czf $backup_dir/mysql.tar.gz $backup_dir/mysql/

You will want to edit the dblist and the backup_dir to point to a directory that exists, and also create a mysql directory under it.


My MySQL Backup Script was first published on October 12, 2005.

Discuss / Follow me on Twitter ↯