Created
January 24, 2012 16:13
-
-
Save servergrove/1670895 to your computer and use it in GitHub Desktop.
Script to backup MySQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Configuration. | |
date=`date +%Y-%m-%d` | |
bk_dest='/var/archives/mysql' | |
log_file=$bk_dest/bk_mysql-${date}.log | |
mysql_cmd='/usr/bin/mysql' | |
mysqldump_cmd='/usr/bin/mysqldump' | |
dbuser=root | |
dbpass=`cat /etc/.mysqlpasswd` | |
databases=(`echo 'show databases;' | $mysql_cmd -u ${dbuser} --password=${dbpass} | grep -v ^Database$`) | |
for d in "${databases[@]}"; do | |
if [[ $d != 'tmp' && $d != 'test' ]] | |
then | |
echo "DATABASE ${d}" >> $log_file | |
path="${bk_dest}/${date}" | |
mkdir -p ${path} | |
${mysqldump_cmd} --user=${dbuser} --password=${dbpass} --opt --databases ${d} | bzip2 -c > ${path}/${d}.sql.bz2 | |
fi | |
done | |
# delete old dumps (retain 5 days) | |
find ${bk_dest} -mtime +5 -exec rm {} \; | |
echo "" >> $log_file | |
echo Disk Space Report: >> $log_file | |
echo -------------------------------------- >> $log_file | |
du -h --max-depth=1 $bk_dest >> $log_file |
Ok, it was the fault of the fact that /bin/sh
was a link to dash
not bash
.
For posterity will, resolves itself into this:
after installing bash with:
sudo apt-get install bash
give the commands:
sudo rm /bin/sh
sudo ln-s /bin/bash /bin/sh
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
this script doesn't work! Since error at line 11:
/etc/cron.daily/backup_mysql.sh: 11: /etc/cron.daily/backup_mysql.sh: Syntax error: "(" unexpected
But if I remove the parentheses gives me this error:
/etc/cron.daily/backup_mysql.sh: 13: /etc/cron.daily/backup_mysql.sh: Bad substitution
I have a vps with ubuntu 12.04.