备份方法scp rsync,inotifywait

参考http://blog.leezhong.com/project/2010/12/13/inotify-rsync.html
https://github.com/rvoicilas/inotify-tools/wiki/
远程执行脚本
ssh -l root 10.10.10.10 "/opt/test/stop.sh";
ssh -l root 10.10.10.10  "scp /opt/data/database.tcb 10.10.10.10:/opt/ttserver/test/test.tcb"

rsync+inotifywait
#!/bin/sh
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format  '%T %w%f %e' \
 -e modify,create,move  \
/opt/cms5/share/template/cms_page/template/ \
| while read date time file event
        do
                rsync -av --progress  --include=kk  --exclude='*' --password-file=/etc/rsyncd.secrets /opt/backuptest/ [email protected]::test >>/opt/log/templatenotify.log
                #echo "${date} ${time} ${file} ${event}"
        done


#!/bin/sh
#/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format  '%T %w %f %e' \
/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format  '%T %w %f %e' \
 -e modify,create,move  \
/opt/backuptest/ \
| while read date time path file event
        do
#               rsync -av --password-file=/etc/rsyncd.secrets /opt/rsynctest/ [email protected]::rsynctest >>/tmp/rsync.log
                rsync --include='${file}' --exclude='*' -av  ${path}  [email protected]:${path}
                echo "${date} ${time} ${path} ${file} ${event}"
        done

--------templatenotify.sh----------
#!/bin/sh
#this is a test  not used for production ,please look 154 
logfile="/opt/log/templatenotify-`date +%Y%m%d`.log"
toip=10.11.10.10
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format  '%T %w%f %e' \
 -e CLOSE_WRITE,delete,create,move  \
/opt/template/ \
| while read date time  file event
do
case "$event" in
        DELETE)
        echo "delete ${file} but not rsync ----delete--- over" >>${logfile};
        ;;
        *)
        if [ "${file: -4}" != '4913' ]  && [ "${file: -1}" != '~' ]; then
                echo "rsync -av  $file  root@${toip}:`dirname $file`" >>${logfile}
                rsync -av  $file  root@${toip}:`dirname $file`  >>${logfile} 2>&1
                echo "${date}  ${time}  ${file} ${event} --------over" >>${logfile}
        fi
        ;;
esac
done

定时删除无用文件比如log
crontab -l
0 3 * * * (find /opt/modules/ -type f -mtime +2 -exec rm -f {} \;)

[@tc_157_49 share]# cat /usr/local/src/switch/templatenotify.sh
#!/bin/sh
#this is a test  not used for production ,please look 154 
logfile="/opt/log/templatenotify-`date +%Y%m%d`.log"
toip=10.11.157.53
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format  '%T %w%f %e' \
 -e CLOSE_WRITE,delete,create,move  \
/opt/cms5/share/template/cms_page/template/ \
| while read date time  file event
do
case "$event" in
        DELETE)
        echo "delete ${file} but not rsync ----delete--- over" >>${logfile};
        ;;
        *)
        if [ "${file: -4}" != '4913' ]  && [ "${file: -1}" != '~' ]; then
                echo "rsync -av  $file  root@${toip}:`dirname $file`" >>${logfile}
                rsync -av  $file  root@${toip}:`dirname $file`  >>${logfile} 2>&1
                echo "${date}  ${time}  ${file} ${event} --------over" >>${logfile}
        fi
        ;;
esac
done
[@tc_157_49 share]# 

你可能感兴趣的:(ssh,脚本,inotify)