德国开元华人社区 开元周游
标题:
简单实现服务器资料自动备份
[打印本页]
作者:
taohui
时间:
9.4.2010 04:35
标题:
简单实现服务器资料自动备份
示例1:实现mysql数据库的自动备份
每天自动在凌晨2点自动备份数据库名为dbname的数据,下面设置导出方式为:mysqldump,导出后自动打包放到backup目录下,并删除10天以前的备份文件
#!/bin/bash
#This is a ShellScript For Auto DB Backup
#Powered by taohuifive@hotmail.com
#2005-09
#Setting
DBName=dbname
DBUser=root
DBPasswd=123456
BackupPath=/backup/
LogFile=/backup/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='10 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
复制代码
把以上代码保存文件名为DBbakcup.sh放在/home/ 下
在命令行下输入crontab -e
0 2 * * * sh /home/DBbakcup.sh
示例2:实现网站文件的自动备份
每天自动在凌晨3点自动把网站的所有内容打包,自动压缩打包后放到backup目录下,并自动删除10天以前的备份文件
#!/bin/bash
#This is a ShellScript For Auto site Backup
#Powered by taohuifive@hotmail.com
#2007-01
#Setting
BackupPath=/backup/
LogFile=/backup/site.log
SitePath=/var/www/htdocs/
NewFile="$BackupPath"site$(date +%y%m%d).tgz
DumpFile="$BackupPath"site$(date +%y%m%d)
OldFile="$BackupPath"site$(date +%y%m%d --date='10 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
tar czvf $NewFile $SitePath$siteName >> $LogFile 2>&1
echo "[$NewFile]Backup Success!" >> $LogFile
fi
echo "-------------------------------------------" >> $LogFile
复制代码
把以上代码保存文件名为SITEbakcup.sh放在/home/ 下
在命令行下输入crontab -e
0 3 * * * sh /home/SITEbakcup.sh
作者:
素灰
时间:
9.4.2010 05:53
有机会试一下……
作者:
萝卜土豆
时间:
9.4.2010 23:55
这个给萍聚的技术人员看看不错。。。。。。。。。。
欢迎光临 德国开元华人社区 开元周游 (https://forum.kaiyuan.cn/)
Powered by Discuz! X3.2