CentOS7 CronTab 计划任务

一个自动备份的脚本

线上的,将相关网站和数据库备份

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点整开始将网上的备份下载到本地备份服务器
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值