The script can be put into crontab, the early morning once a day, automatic backup of this script only once a day, and only the most recent five-day backup on the server. Note: DumpFile = db $ (date +% y% m% d) if this must be set to release this script do the backup directory. DumpFile = “$ BackupPath” db $ (date +% y% m% d) if this is set to log in will be prompted to do tar: Removing leading ‘/’ from member names is because the backup directory using the absolute path , but this does not affect the data, can be determined according to their own habits.
#!/bin/bash
#This is a ShellScript For Auto DB Backup
#Powered by cpworld2000.com
#Setting
# Set the database name, database login name, password, backup path, log path, data file location, as well as backup
# By default the backup is tar, also can be a mysqldump, mysqldotcopy
# By default, the root (empty) log mysql database backup to /root/dbxxxxx.tgz
DBName=mysql
DBUser=root
DBPasswd=
BackupPath=/root/
LogFile=/root/db.log
DBPath=/var/lib/mysql/
#BackupMethod=mysqldump
#BackupMethod=mysqlhotcopy
#BackupMethod=tar
#Setting End
NewFile=”$BackupPath”db$(date +%y%m%d).tgz
DumpFile=”$BackupPath”db$(date +%y%m%d)
OldFile=”$BackupPath”db$(date +%y%m%d –date=’5 days ago’).tgz
echo “——————————————-” >> $LogFile
echo $(date +”%y-%m-%d %H:%M:%S”) >> $LogFile
echo “————————–” >> $LogFile
#Delete Old File
if [ -f $OldFile ]
then
rm -f $OldFile >> $LogFile 2>&1
echo “[$OldFile]Delete Old File Success!” >> $LogFile
else
echo “[$OldFile]No Old Backup File!” >> $LogFile
fi
if [ -f $NewFile ]
then
echo “[$NewFile]The Backup File is exists,Can’t Backup!” >> $LogFile
else
case $BackupMethod in
mysqldump)
if [ -z $DBPasswd ]
then
mysqldump -u $DBUser –opt $DBName > $DumpFile
else
mysqldump -u $DBUser -p$DBPasswd –opt $DBName > $DumpFile
fi
tar czvf $NewFile $DumpFile >> $LogFile 2>&1
echo “[$NewFile]Backup Success!” >> $LogFile
rm -rf $DumpFile
;;
mysqlhotcopy)
rm -rf $DumpFile
mkdir $DumpFile
if [ -z $DBPasswd ]
then
mysqlhotcopy -u $DBUser $DBName $DumpFile >> $LogFile 2>&1
else
mysqlhotcopy -u $DBUser -p $DBPasswd $DBName $DumpFile >>$LogFile 2>&1
fi
tar czvf $NewFile $DumpFile >> $LogFile 2>&1
echo “[$NewFile]Backup Success!” >> $LogFile
rm -rf $DumpFile
;;
*)
/etc/init.d/mysqld stop >/dev/null 2>&1
tar czvf $NewFile $DBPath$DBName >> $LogFile 2>&1
/etc/init.d/mysqld start >/dev/null 2>&1
echo “[$NewFile]Backup Success!” >> $LogFile
;;
esac
fi
echo “——————————————-” >> $LogFile

No Responses to “Mysql automatic backup script”