需求:python
win主機上一個目錄爲備份目錄,天天都會有增量文件,而咱們要將這些增量文件所有copy到異地linux主機,以下圖linux
win主機:shell
一、安裝 rsync 客戶端 (cwRsync)windows
二、同步腳本bash
linux主機:服務器
一、安裝rsync服務便可ide
linux配置:測試
linux的rsync的配置以下:spa
[tfsbackup] path=/data/tfsbackup comment=tfsbackup ignore errors = yes read only=no write only=no hosts allow=* hosts deny=* list=false auth users=rsync secrets file=/etc/rsyncd.secrets
win配置:.net
一、安裝 cwRsync (附件中能夠下載 或 https://www.itefix.net/cwrsync)
將文件解壓,變量配置便可,命令行測試經過便可。
二、同步腳本以下:腳本名暫定 tfs.py
#!/usr/bin/env python # -*- coding:utf-8 -*- """ filename:tfs.py function:檢索指定目錄下的1天內的文件,同步到服務器上。 """ import os import time import glob import subprocess import os.path expires = 172800 #時間戳差值在2天,爲了保險會校驗兩天內的增量文件 currentTime=int(time.time()) #獲取當前時間戳 args=r'-az' remotehost =r'rsync@10.168.0.59::tfsbackup' passwordFile=r'--password-file=/cygdrive/c/cwrsync/rsyncd.secrets' path = r'E:\tfs\*' comm=r'c:\cwRsync\rsync.exe' #以上爲rsync最後的命令表現形式;/cygdrive/c/cwrsync/rsyncd.secrets表示c盤下的cwrsync/rsyncd.secrets文件。 srcpath=r'/cygdrive/e/tfs/' #/cygdrive/e/表示windows系統的E盤, filelist = glob.glob(path) #獲取給定路徑下全部文件名 #過濾出最近2天內的文件, def checkFileTime(filename): fileTime = os.stat(filename).st_mtime if (currentTime - fileTime > expires): pass else: filename = os.path.basename(filename) srcfile=srcpath+filename rsyncFile(srcfile) #rsync到遠程主機上 def rsyncFile(file): cmd = "%s %s %s %s %s" %(comm,args,file,remotehost,passwordFile) result = subprocess.call(cmd) if result != 0: print "rsync to " , remotehost , " fail ! !" time.sleep(10) else: print file , "rsync to " , remotehost , "successful !" for filename in filelist: checkFileTime(filename)
由於備份的win主機上沒有權限安裝Python環境。因此轉換到exe執行程序就能夠了。
PyInstaller恰知足這個需求。本文PyInstaller的版本是2.0,支持Python2.7。下面討論怎樣安裝,使用PyInstaller。
PyInstaller自己並不屬於Python包。在安裝 pyinstaller 以前假設你已經安裝了python ,注意把python 環境變量配置好, 即 進入cmd後 輸入 python 會進入 python shell.
pyinstaller安裝:
1,下載pyinstaller並解壓(能夠去官網下載最新版)
C:\pyinstaller-2.0
2,安裝最新版本的 pywin32-217.win32-py2.7.exe:
否則會出現錯誤Error: PyInstaller for Python 2.6+ on Windows needs pywin32.
3、
C:\Users\xisuo>cd C:\pyinstaller-2.0 C:\pyinstaller-2.0>python pyinstaller.py -w --onefile D:\py\tfs.py
上面的命令成功運行後tfs.py 會生成一個tfs文件夾。在這個文件夾下面會有一個名爲dist的文件夾,此文件夾下面有轉換好的tfs.exe。
當你執行文件tfs.exe便可rsync到遠程主機上。作計劃任務便可。