Home > Article > Backend Development > mysql automatic backup----aspbiz_PHP tutorial
I wrote a Mysql backup script. Let's take a look and see if there is anything wrong with it.
This is the first Shell script I wrote. Please help me take a look.
If anyone needs it, I can send it to him.
You can put this script into crontab and execute it once every morning for automatic backup.
This script can only be executed once a day at most, and only the backups of the last five days will be kept on the server.
Code:
[code]
#!/bin/bash
#This is a ShellScript For Auto DB Backup
#Powered by aspbiz
#2004-09
# Setting
#Set the database name, database login name, password, backup path, log path, data file location, and backup method
#The backup method is tar by default, and it can also be mysqldump, mysqldotcopy
# By default, use root (empty) to log in to the mysql database and back up 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