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