shell编程,跨服务器备份文件

需求:查询某个文件夹下的所有文件,将文件修改时间小于当前时间,并大于当前时间前一天的文件备份到另一台服务器对应的文件夹下

思路:

1、递归查询文件夹下的文件

2、如果文件夹中含有空格,则将文件按列显示,并将IFS设为 \x0A

代码如下:

#! /bin/bash
function read_dir(){
    IFS=$'\x0A'
        executeDate=`date -d ' -1 day ' +%F" "%T`
        executeDate1=`date -d "${executeDate}" +%s`

    for file in `ls $1 | paste`
    do
        modifyDate=`stat $1"/"$file -c %y`
            currentDate=`date +%F" "%T`
            currentDate1=`date -d "${currentDate}" +%s`
            modifyDate1=`date -d "${modifyDate}" +%s`

        if [ -d $1"/"$file ]
        then
            read_dir $1"/"$file
        elif [ $modifyDate1 -lt $currentDate1 ] && [ $modifyDate1 -gt $executeDate1 ];
        then 
                scp -r "$1""/" "$ip:"$path
        fi
    done
}

path=/root/hu
ip=root@192.168.11.66
read_dir $path $ip

 

posted @ 2017-11-20 13:45  Alighieri  Views(337)  Comments(0Edit  收藏  举报