2011-10-04, 06:59
Here you go, something like this aught to work:
Set the number of backups you want (defaut 3)
Set the databases you want (default XBMC_Video and XBMC_Music)
Set the backup path (default is execution directory +backup/)
Modify the backup timestamp if you want it, default is YYMMDD
When it dumps you should end up with something like this: XBMC_Video.111004.00.sql (00 is the newest, 02 the oldest)
Script automatically deletes the oldest backup.
Also, make sure you've setup your my.cnf, that's how the dumper works, or add in the -U -P switches to the mysqldump command.
Can't guarantee this will work, I've ripped it out of a larger backup script I've written to dump and backup over rsync several dozen postgres databases, but the foundation is there anyways.
Set the number of backups you want (defaut 3)
Set the databases you want (default XBMC_Video and XBMC_Music)
Set the backup path (default is execution directory +backup/)
Modify the backup timestamp if you want it, default is YYMMDD
When it dumps you should end up with something like this: XBMC_Video.111004.00.sql (00 is the newest, 02 the oldest)
Script automatically deletes the oldest backup.
Also, make sure you've setup your my.cnf, that's how the dumper works, or add in the -U -P switches to the mysqldump command.
Code:
#!/bin/bash
# Set Variables
dbcount=3
dbnames=( "XBMC_Video" "XBMC_Music" )
backup_path="backup/"
long_date=`date`
short_date=`date +"%y%m%d"`
date_regex="[0-9]{6}"
# Start backup process
#echo ""
echo "Starting backup - "$long_date
# Dumping Databases
# echo ""
# echo "Dumping databases"
# echo ""
cd /$backup_path
for (( i = 0 ; i <= `expr ${#dbnames[@]} \- 1`; i++ ))
do
# echo " -Moving old backups"
for (( c = `expr $dbcount \- 1` ; c >=0; c-- ))
do
n=`expr $c \+ 1`
while test "${#n}" -lt 2
do
n="0$n"
done
d=$c
while test "${#d}" -lt 2
do
d="0$d"
done
file=`ls | grep -E ".($d).(sql)$"`
# echo "File name is: $file"
if [[ $file =~ $date_regex ]]
then
if [ -f "$file" ]
then
# echo "Moving $file to ${dbnames[$i]}.${BASH_REMATCH[0]}.$n.sql"
mv $file ${dbnames[$i]}.${BASH_REMATCH[0]}.$n.sql
fi
fi
done
# echo " -Dumping ${dbnames[$i]} to ${dbnames[$i]}.$short_date.00.sql"
# mysqldump -B ${dbnames[$i]} > ${dbnames[$i]}.$short_date.00.sql
old_file=`ls | grep -E ".($dbcount).(sql)$"`
if [[ $old_file =~ $date_regex ]]
then
if [ -f "$old_file" ]
then
# echo " -Removing oldest backup: $old_file"
rm $old_file
fi
fi
done
# Complete
# echo ""
echo "Backup Complete - "$long_date
Can't guarantee this will work, I've ripped it out of a larger backup script I've written to dump and backup over rsync several dozen postgres databases, but the foundation is there anyways.