cat /opt/inotify_rsync.sh
#!/bin/bash
export RSYNC_PASSWORD=JxwKRl%!xxxxxx
# 实时监控 61.212 主机/sftp/ 的变化
INOTIFY_CMD="/usr/local/bin/inotifywait -mrq -e modify,create,attrib,move,delete /sftp/"
#
RSYNC_CMD="rsync -azH --delete /sftp/ [email protected]::backup"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
if [ $(pgrep rsync | wc -l) -le 5 ] ; then
$RSYNC_CMD
fi
done
cat /tmp/rsync_user.sh
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
export PATH
# 合并变量声明
export RSYNC_PASSWORD='Jxxxxxx8' \
USER='vmusexxxxr' \
PASS='JxwKRl%!UrxxxN8' \
root_password='qA1!wS2xxxxxrF4$' \
backup_host='172.xxxx.61.242' \
path_file='backup_userfile'
# 同步用户密码的方法
funuser() {
command_to_execute="cat /tmp/passwd >/etc/passwd; cat /tmp/shadow >/etc/shadow; cat /tmp/group >/etc/group"
expect << EOF
set timeout 30
spawn ssh $USER@$backup_host
expect {
"yes/no" { send "yes\r"; exp_continue }
"password:" { send "$PASS\r" }
}
expect "$USER@*" {
send "su - root\r"
expect "Password:"
send "$root_password\r"
expect "#"
send "$command_to_execute\r"
expect "#"
send "exit\r"
}
expect "$user@*" { send "exit\r" }
expect eof
EOF
}
# 判断vip是否在这台主机上
vip=$(ip a | grep -w "172.25.61.188/32" | awk '{print $2}')
if [[ $vip == "172.25.61.188/32" ]]; then
rsyncagent=$(ps -elf | grep inotify_rsync.sh | wc -l)
if [[ $rsyncagent -eq 1 ]]; then
sh /opt/inotify_rsync.sh &
sleep 2
fi
echo "任务开始"
cat /etc/passwd > /tmp/passwd
cat /etc/group > /tmp/group
cat /etc/shadow > /tmp/shadow
rsync -azH --delete /tmp/passwd /tmp/group /tmp/shadow backuper@$backup_host::$path_file
if [[ $? -eq 0 ]]; then
funuser
else
break
fi
echo "任务结束"
else
ps -elf | grep inotify_rsync.sh | grep -v 'color=auto' | awk '{print $4}' | xargs kill
fi
标签:脚本,tmp,rsync,同步,group,用户,cat,bin,shadow
From: https://blog.51cto.com/zhaochengsheng/6499495