歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux教程

Solaris下用scp自動同步文件

1. scp同步文件腳本 //syntest.sh

#!/bin/bash

# Setting cdm path is used to store client software
host_dir="/var/hostdir"
# Statement the file is used to store the size of cdm folder size
fsrecord=".r"
# To synchronize the target machine ip
client_ip="192.168.0.2"
# To synchronize the target machine directory
client_dir="/var/clientdir"

# Current file folder size
new_folder_size="$(du -sh $host_dir |awk '{print $1}')"
# Read the size of the folder and remove character 'M'
if [ ! -f $fsrecord ]
then
   echo "0M" > $fsrecord
fi
old_folder_size="$(cat $fsrecord|tr -d 'M')"
# Remove character 'M'
temp_new="$(echo $new_folder_size|tr -d 'M')"

# Compare file folder size
if [ "$temp_new" != "$old_folder_size" ]
then
   scp -r $host_dir $client_ip:$client_dir
   echo ""
   echo "-----The folder has been synchronized success!-----"
   # Write the latest file folder size
   echo $new_folder_size > $fsrecord
else
   echo "-----The folder has not been changed, so it does not need to synchronize.-----"
fi

2. 為scp自動拷貝生成不用輸入密碼的密匙

 參考《ssh(scp)自動登錄的幾種方法》

3. 用crontab定時運行腳本。

 1.) 將syntest.sh拷貝到/etc目錄下

 2.) 編輯crontab,文件在/var/spool/cron/crontabs下

      比如:每分鐘運行一次

      0-59 * * * * /etc/syntest.sh  //*/5    每5分鐘1次  */20    每20分鐘1次   在Linux下可以,但是在Solaris下不支持

4. refresh crontab

    svcadm refresh cron

Copyright © Linux教程網 All Rights Reserved