发布网友 发布时间:2024-10-22 05:18
共1个回答
热心网友 时间:2024-10-22 09:09
使用脚本每天定期执行数据库备份操作,对每个使用mysql数据库的人来说都很有必要,这样的脚本网上有很多,这里摘抄一个朋友的脚本 dbbackup:%D%A这个脚本每天最多只执行一次,而且只保留最近五天的备份在服务器上。%Dbbackup代码:%D%A#!/bin/bash%D%A#This is a ShellScript For Auto DB Backup%D%A#Powered by aspbiz%D%A#设置数据库名,数据库登录名,密码,备份路径,日志路径,数据文件位置,以及备份方式%D%A#默认情况下备份方式是tar,还可以是mysqldump,mysqldotcopy%D%A#默认情况下,用root(空)登录mysql数据库,备份至/root/dbxxxxx.tgz%DBName=mysql%DBUser=root%DBPasswd=%D«ackupPath=/root/%D%ALogFile=/root/db.log%DBPath=/var/lib/mysql/%D%A#BackupMethod=mysqldump%D%A#BackupMethod=mysqlhotcopy%D%A#BackupMethod=tar%D%A#Setting End%D%ANewFile="$BackupPath"db$(date +%y%m%d).tgz%DumpFile="$BackupPath"db$(date +%y%m%d)%D%AOldFile="$BackupPath"db$(date +%y%m%d --date='5 days ago').tgz%D®cho "-------------------------------------------" >> $LogFile%D®cho $(date +"%y-%m-%d %H:%M:%S") >> $LogFile%D®cho "--------------------------" >> $LogFile%D%A#Delete Old File%D%Aif [ -f $OldFile ]%D%Athen%D%Arm -f $OldFile >> $LogFile 2>&1%D®cho "[$OldFile]Delete Old File Success!" >> $LogFile%D®lse%D®cho "[$OldFile]No Old Backup File!" >> $LogFile%D¯i%D%Aif [ -f $NewFile ]%D%Athen%D®cho "[$NewFile]The Backup File is exists,Can't Backup!" >> $LogFile%D®lse%D¬ase $BackupMethod in%D%Amysqldump)%D%Aif [ -z $DBPasswd ]%D%Athen%D%Amysqldump -u $DBUser --opt $DBName > $DumpFile%D®lse%D%Amysqldump -u $DBUser -p$DBPasswd --opt $DBName > $DumpFile%D¯i%D%Atar czvf $NewFile $DumpFile >> $LogFile 2>&1%D®cho "[$NewFile]Backup Success!" >> $LogFile%D%Arm -rf $DumpFile%D%A;;%D%Amysqlhotcopy)%D%Arm -rf $DumpFile%D%Amkdir $DumpFile%D%Aif [ -z $DBPasswd ]%D%Athen%D%Amysqlhotcopy -u $DBUser $DBName $DumpFile >> $LogFile 2>&1%D®lse%D%Amysqlhotcopy -u $DBUser -p $DBPasswd $DBName $DumpFile >>$LogFile 2>&1%D¯i%D%Atar czvf $NewFile $DumpFile >> $LogFile 2>&1%D®cho "[$NewFile]Backup Success!" >> $LogFile%D%Arm -rf $DumpFile%D%A;;%D%A*)%D%A/etc/init.d/mysqld stop >/dev/null 2>&1%D%Atar czvf $NewFile $DBPath$DBName >> $LogFile 2>&1%D%A/etc/init.d/mysqld start >/dev/null 2>&1%D®cho "[$NewFile]Backup Success!" >> $LogFile%D%A;;%D®sac%D¯i%D®cho "--------------" >> $LogFile