两个mysql备份的脚本
时间:2007-08-02 来源:jacky.lee
一、mysql单数据库备份并ftp的shell脚本
#!/bin/sh
############### mysql One DataBase's Backup & FTP ################
#1. mkdir /home/backup
#2. chmod 700 DBbackup.sh
#3. vi /etc/crontab , 30 03 * * * root /home/backup/DBbackup.sh
#everyday 03:30 the DBbackup.sh will work #System Setup #ftp Info
host=192.168.0.3 #ftp host
UserName=ftp #ftp user name
Passwd=ftp #fto user password #mysql Info
SQL_host=localhost #mysql host
SQL_User=root #mysql UserName
SQL_Passwd=abc #User Password
SQL_db=test #database name
backup_path=/home/backup #set saved backup path
file=$SQL_db-$(date +%Y%m%d).tar.gz
mysql_path=/usr/local/mysql/bin #set the mysql database bin path cd $backup_path
#export database
$mysql_path/mysqldump -h $SQL_host -u$SQL_User -p$SQL_Passwd -B $SQL_db>$SQL_db.dump sleep 5s
#file tar
tar -czf $backup_path/$file $SQL_db.dump sleep 10s cd $backup_path
sleep 1s
#file ftp
ftp -i -n <open $host
user $UserName $Passwd
put $file
bye
! sleep 10s
rm -rf $backup_path/$file
rm -rf $backup_path/$SQL_db.dump 二、一个mysql数据库自动备份脚本
可以将这个脚本放进crontab,每天凌晨执行一次,自动备份这个脚本每天最多只执行一次,而且只 保留最近五天的备份在服务器上。 代码如下: #!/bin/sh
#this is a shellscript for auto db backup
#2007--05-23 #setting
#设置数据库名,数据库登录名,密码,备份路径,日志路径,数据文件位置,以及备份方式
#默认情况下备份方式是tar,还可以是mysqldump,mysqldotcopy
#默认情况下,用root(空)登录mysql数据库,备份至/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
then
rm -f $oldfile >> $logfile 2>&1
echo "delete old file success!" >> $logfile
else
echo "no old backup file!" >> $logfile
fi if
then
echo "the backup file is exists,can"t backup!" >> $logfile
else
case $backupmethod in
mysqldump)
if
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 "backup success!" >> $logfile
rm -rf $dumpfile
;;
mysqlhotcopy)
rm -rf $dumpfile
mkdir $dumpfile
if
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 "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 "backup success!" >> $logfile
;;
esac
fi echo "-------------------------------------------" >> $logfile
#2. chmod 700 DBbackup.sh
#3. vi /etc/crontab , 30 03 * * * root /home/backup/DBbackup.sh
#everyday 03:30 the DBbackup.sh will work #System Setup #ftp Info
host=192.168.0.3 #ftp host
UserName=ftp #ftp user name
Passwd=ftp #fto user password #mysql Info
SQL_host=localhost #mysql host
SQL_User=root #mysql UserName
SQL_Passwd=abc #User Password
SQL_db=test #database name
backup_path=/home/backup #set saved backup path
file=$SQL_db-$(date +%Y%m%d).tar.gz
mysql_path=/usr/local/mysql/bin #set the mysql database bin path cd $backup_path
#export database
$mysql_path/mysqldump -h $SQL_host -u$SQL_User -p$SQL_Passwd -B $SQL_db>$SQL_db.dump sleep 5s
#file tar
tar -czf $backup_path/$file $SQL_db.dump sleep 10s cd $backup_path
sleep 1s
#file ftp
ftp -i -n <open $host
user $UserName $Passwd
put $file
bye
! sleep 10s
rm -rf $backup_path/$file
rm -rf $backup_path/$SQL_db.dump 二、一个mysql数据库自动备份脚本
可以将这个脚本放进crontab,每天凌晨执行一次,自动备份这个脚本每天最多只执行一次,而且只 保留最近五天的备份在服务器上。 代码如下: #!/bin/sh
#this is a shellscript for auto db backup
#2007--05-23 #setting
#设置数据库名,数据库登录名,密码,备份路径,日志路径,数据文件位置,以及备份方式
#默认情况下备份方式是tar,还可以是mysqldump,mysqldotcopy
#默认情况下,用root(空)登录mysql数据库,备份至/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
then
rm -f $oldfile >> $logfile 2>&1
echo "delete old file success!" >> $logfile
else
echo "no old backup file!" >> $logfile
fi if
then
echo "the backup file is exists,can"t backup!" >> $logfile
else
case $backupmethod in
mysqldump)
if
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 "backup success!" >> $logfile
rm -rf $dumpfile
;;
mysqlhotcopy)
rm -rf $dumpfile
mkdir $dumpfile
if
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 "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 "backup success!" >> $logfile
;;
esac
fi echo "-------------------------------------------" >> $logfile
相关阅读 更多 +