更新时间:2022-09-15 09:02:42
需求:
win主机上一个目录为备份目录,每天都会有增量文件,而我们要将这些增量文件全部copy到异地linux主机,如下图
win主机:
1、安装 rsync 客户端 (cwRsync)
2、同步脚本
linux主机:
1、安装rsync服务即可
linux配置:
linux的rsync的配置如下:
1
2
3
4
5
6
7
8
9
10
11
|
[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配置:
1、安装 cwRsync (附件中可以下载 或 https://www.itefix.net/cwrsync)
将文件解压,变量配置即可,命令行测试通过即可。
2、同步脚本如下:脚本名暂定 tfs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
#!/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并解压(可以去官网下载最新版)
1
|
C:\pyinstaller-2.0 |
2,安装最新版本的 pywin32-217.win32-py2.7.exe:
不然会出现错误Error: PyInstaller for Python 2.6+ on Windows needs pywin32.
3、
1
2
|
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到远程主机上。做计划任务即可。
本文转自 西索oO 51CTO博客,原文链接:http://blog.51cto.com/lansgg/1834363