今天继续维护网站,目的主要是为了能自动备份数据库和网站目录文件,然后使用crond服务设定计划任务成每天定时执行。写完shell脚本实验了一下感觉还不错,特把脚本文件共享出来以供参考。
首先是自动备份数据库的脚本:
#!/bin/bash #!/bin/bash #This is a ShellScript For Auto DB Backup #Setting #设置数据库名,数据库登录名,密码,备份路径,日志路径,数据文件位置,以及备份方式 #默认情况下备份方式是tar,还可以是mysqldump #默认情况下,用root(空)登录mysql数据库,备份至/root/dbxxxxx.tar.gz #mysqldump情况下,会重启httpd服务 DBName=db DBUser=root DBPasswd= BackupPath=/root/ LogFile=/root/db.log DBPath=/var/lib/mysql/ BackupMethod=mysqldump #BackupMethod=tar #Setting End NewFile="$BackupPath"db$(date +%y%m%d).tar.gz DumpFile=db$(date +%y%m%d).sql OldFile="$BackupPath"db$(date +%y%m%d --date='30 days ago').tar.gz 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) cd $BackupPath service httpd stop >> $LogFile 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 service httpd start >> $LogFile ;; *) /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
随后是自动备份网站目录的脚本:
#!/bin/bash #!/bin/bash #This is a ShellScript For Web Files Backup #Setting #设置备份路径,日志路径,web文件位置 #默认情况下,备份至/root/webxxxxx.tar.gz BackupPath=/root/ LogFile=/root/web.log WebPath=/var/www/html/ #Setting End NewFile="$BackupPath"web$(date +%y%m%d).tar.gz OldFile="$BackupPath"web$(date +%y%m%d --date='30 days ago').tar.gz 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 cd $WebPath tar czvf $NewFile . >> $LogFile 2>&1 echo "[$NewFile]Backup Success!" >> $LogFile fi echo "-------------------------------------------" >> $LogFile
最后就是cron脚本,这个比较简单,修改/etc/crontab文件就可以了:
SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/ # For details see man 4 crontabs # Example of job definition: # .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | | # * * * * * user-name command to be executed 0 4 * * * root /root/dbbackup.sh 5 4 * * * root /root/webbackup.sh
需要注意的是crontab修改完后需要重启crond服务,dbback.sh和webbackup.sh文件需要添加可执行权限。
» 转载请注明来源:Terence的窝 » 《定时备份mysql和网站目录文件》