入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

CentOS7 CronTab 计划任务

创建时间:2016-04-28 投稿人: 浏览次数:4864

一个自动备份的脚本

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

backup.sh

#!/bin/sh
#backup localhost sql and website files
dir_name=$(date +%Y%m%d%H%M)
mkdir /backup/$dir_name
mysqldump -uroot -yourpassword 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
"; exp_continue}
"*password*" {send "yourpassword
";}
}
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点整开始将网上的备份下载到本地备份服务器

声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。