Shell Script: Backup and gzip mysql databases for linux systems

This is a quite helpful script to backup your mysql databases. All databases are automaticly gzipped and named like this DATABASENAME.yyyymmdd.gzip. You may execute it as cronjob every night for example.



BACKUP_DIR=${1:-/backups/mysql} # Directory to save backups
DATE=`date "+%Y%m%d"`

# check if directory exists
if [ ! -d $BACKUP_DIR ]; then
        mkdir -p $BACKUP_DIR
        chown 0700 $BACKUP_DIR
for i in /var/lib/mysql/*/; do
   dbname=`basename "$i"`
   mysqldump --user=root --password=YOURPASSWORD "$dbname" | gzip > $BACKUP_DIR/$dbname.$DATE.gz

 # delete all old Backups
 find $BACKUP_DIR -mtime +${EXPIRETIME} -exec rm {} \;

Leave a Comment