本文實例為大家分享了shell跨服務器備份文件的具體代碼,供大家參考,具體內容如下
需求:查詢某個文件夾下的所有文件,將文件修改時間小于當前時間,并大于當前時間前一天的文件備份到另一臺服務器對應的文件夾下
思路:
1、遞歸查詢文件夾下的文件
2、如果文件夾中含有空格,則將文件按列顯示,并將IFS設為 /x0A
代碼如下:
#! /bin/bashfunction 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/huip=root@192.168.11.66read_dir $path $ip
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答