shell编程跨服务器备份文件
程序员文章站
2022-07-06 11:16:50
本文实例为大家分享了shell跨服务器备份文件的具体代码,供大家参考,具体内容如下
需求:查询某个文件夹下的所有文件,将文件修改时间小于当前时间,并大于当前时间前一天...
本文实例为大家分享了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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。