一个自动备份的脚本
线上的,将相关网站和数据库备份
backup.sh
#!/bin/sh
#backup localhost sql and website files
dir_name=$(date +%Y%m%d%H%M)
mkdir /backup/$dir_name
mysqldump -uroot -pYOURPASSWORD xxoo_db>/backup/$dir_name/xxoo_db.sql
tar zcvf /backup/$dir_name/ooxx.tar.gz /www/ooxx
#delete x days before directory
list_alldir(){
for file2 in `ls -a $1`
do
if [ x"$file2" != x"." -a x"$file2" != x".." ];then
if [ -d "$1/$file2" ];then
if [ $file2 -lt $dir ];then
rm -rf $1/$file2
fi
fi
fi
done
}
dir=$(date -d "-1 days" +%Y%m%d%H%M)
list_alldir /backup
<pre name="code" class="cpp">#vi /etc/crontab
0 0 * * * root /sh/backup.sh
以上会自动在每天0点0分开始执行xxoo网站的备份 (线上)
本地备份服务器
download.sh
#backup online's website to localhost
expect -c "
set timeout 21600;
spawn /usr/bin/scp -r 1.2.3.4:/backup/* /backup/
expect {
\"*yes/no*\" {send \"yes\r\"; exp_continue}
\"*password*\" {send \"yourpassword\r\";}
}
expect eof;"
#remove x days before directory
list_alldir(){
for file2 in `ls -a $1`
do
if [ x"$file2" != x"." -a x"$file2" != x".." ];then
if [ -d "$1/$file2" ];then
if [ $file2 -lt $dir ];then
rm -rf $1/$file2
fi
fi
fi
done
}
dir=$(date -d "-7 days" +%Y%m%d%H%M)
list_alldir /backup
#vi /etc/crontab
0 1 * * * root /sh/download.sh
以上会自动在每天1点整开始将网上的备份下载到本地备份服务器