Board index Linux Mysql

Moderator: chandranjoy

MySQL Backup Script with rotation

Postby chandranjoy » Thu Jun 23, 2011 4:29 pm

MySQL db backup script(we can specify the databases):
#!/bin/bash

DESTDIR="/var/backups"
MYSQLDUMP="/usr/bin/mysqldump"
MYSQLARGS=""
MYSQL="/usr/bin/mysql"
MYCNF="$HOME/.my.cnf"
DEBIAN_CNF="/etc/mysql/debian.cnf"

if [ ! -e ${DESTDIR} ]; then
mkdir -p $DESTDIR || exit 1
fi

if [ ! -f "$MYSQLDUMP" ]; then
exit 1
fi

if [ ! -f "$MYCNF" ]; then
if [ ! -f "$DEBIAN_CNF" ]; then
exit 1
fi
MYSQLARGS="--defaults-extra-file=$DEBIAN_CNF"
fi

#rm -f ${DESTDIR}/*

CURDATE=`date "+%Y%m%d"`

DATABASES=`echo "show databases" | $MYSQL -u root -pJjahpape6 $MYSQLARGS | grep -v -E "^Database$"|egrep "db1|db2"`
for database in ${DATABASES}; do
$MYSQLDUMP -u root -pJjahpape6 $MYSQLARGS "$database" | gzip -9 > "$DESTDIR/$database-$CURDATE.gz"
done



To remove the backup files which are older than 7days:

find /var/backups/ -type f -mtime +7 | xargs rm -vf
chandranjoy
Site Admin
 
Posts: 283
Joined: Fri Oct 23, 2009 11:19 pm

Return to Mysql

Who is online

Users browsing this forum: No registered users and 1 guest


cron