1. 創建配置文件
配置文件爲不存在需要自己新建
mkdir /etc/rsyncd touch /etc/rsyncd/rsyncd.conf #主配置文件; touch /etc/rsyncd/rsyncd.secrets #用戶名密碼配置文件; touch /etc/rsyncd/rsyncd.motd #連接時提示信息 chmod 600 /etc/rsyncd/rsyncd.secrets #將密碼權限修改,增加安全性 echo "wys:123456" >> /etc/rsyncd/rsyncd.secrets #寫入賬號和密碼,注意這裏的賬號必須是系統賬號,而密碼是自定義密碼 vim /etc/rsyncd/rsyncd.conf uid = root #運行rsync的用戶 gid = root #運行rsync的用戶組 use chroot = yes #是否chroot到訪問目錄 max connections = 4 #最大併發數 port = 873 #運行端口 #motd file = /etc/rsyncd/rsyncd.motd #連接時提示信息 log file = /var/log/rsyncd.log #指定保存日誌文件 pid file = /var/run/rsyncd.pid #運行Pid文件 [wys] #模塊名稱 path = /tmp/test #同步目錄 comment = a test #註釋 ignore errors #忽略--delete導致的I/O錯誤 hosts allow = 192.168.1.0/24 #該模塊允許IP hosts deny = 0.0.0.0/32 #該模塊禁止的IP auth users = wys #該模塊認證的用戶 secrets file = /etc/rsyncd/rsyncd.secrets #登陸用戶名密碼
2.創建啓動腳本
[root@xfwy shell]# vim rsyncd.py #!/usr/bin/env python # chkconfig: 35 24 96 # # Author: Eason Wang # Contact: [email protected] # # Date: 2015.11.10 # Ver: 1.0 # Use: # cp rsyncd.py /etc/init.d/rsyncd # chkconfig --add rsyncd # service rsyncd [start|stop|restart|reload] # Config: /etc/rsyncd/rsyncd.conf # # Exit code: # 0 ==> EXIT_SUCCESS # 1 ==> EXIT_FAILURE import sys,os,commands,getopt,signal rsync = '/usr/bin/rsync' CFILE = '/etc/rsyncd/rsyncd.conf' PFILE = '/var/run/rsyncd.pid' prog = 'rsync' daemon = "%s --daemon --config=%s" % (rsync,CFILE) PROCESS = os.popen("ps -ef | grep rsync | grep -v 'grep' | awk '{print $2}'").read().split('\n')[0] if os.path.exists(PFILE): RPFILE = open(PFILE).read().strip() else: RPFILE = '' def start(): if not os.path.exists(rsync): print "FATAL: No such programme" sys.exit(1) if not os.path.exists(CFILE): print "FATAL: config file does not exist" sys.exit(1) print "start %s:" % prog if os.path.exists(PFILE): if PROCESS == RPFILE: print "%s is runing!" % prog else: try: os.remove(PFILE) except: print "[ERROR] pid file Delete failed, Please check!" else: print "Delete the pid file..." start() else : a,b = commands.getstatusoutput(daemon) if a !=0 : print b sys.exit(1) print "[OK]" def stop(): print "stop %s:" % prog try: os.kill(int(RPFILE), signal.SIGKILL) except (IOError, ValueError): print "%s is not runing!" % prog else: if os.path.exists(PFILE): try: os.remove(PFILE) except: print "[ERROR] pid file Delete failed, Please check!" else: print "Delete the pid file..." stop() else: print "[OK]" if __name__ == '__main__': arg = sys.argv[1:] if len(sys.argv[1:]) > 1: print "Too many arguments! Only one!" sys.exit(0) if not arg : print "You must enter parameters!" sys.exit(0) if 'start' in arg: start() sys.exit(0) elif 'stop' in arg: stop() sys.exit(0) elif 'restart' in arg or 'reload' in arg: stop() start() sys.exit(0) else: print "only supports the following parameters: [start|stop|restart|reload]"
自己最近學python,就當練習了,寫寫還是發現,這種腳本用shell寫着簡單~
然後配置chkconfig
cp rsyncd.py /etc/init.d/rsyncd chkconfig --add rsyncd
啓動rsyncd服務
[root@xfwy shell]# service rsyncd start start rsync: [OK]
3.客戶端連接
rsync -avzP [email protected]::wys /tmp/test #格式是user@hostname/IP::Module 最後是同步到本地的文件夾 說明: -a 參數,相當於-rlptgoD,-r 是遞歸 -l 是鏈接文件,意思是拷貝鏈接文件;-p 表示保持文件原有權限;-t 保持文件原有時間;-g 保持文件原有用戶組;-o 保持文件原有屬主;-D 相當於塊設備文件; -z 傳輸時壓縮; -P 傳輸進度; -v 傳輸時的進度等信息,和-P有點關係,自己試試。可以看文檔;
簡單介紹其它同步參數
rsync -avzP --delete [email protected]::wys /tmp/test #表示客戶端上的數據要與服務器端完全一致,如果目錄中有服務器上不存在的文件,則刪除. rsync -avzP --delete --password-file=/root/rsync.password [email protected]::wys /tmp/test #這是當我們以wys用戶登錄rsync服務器同步數據時,密碼將讀取 rsync.password 這個文件,文件保存爲純密碼 即echo "123456" > /root/rsync.password