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

以上是“shell编程如何实现跨服务器备份文件”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!