描述:
1、基于备份服务器部署的py程序,将需要备份主机目录下的内容下载至备份服务器(服务端和远端都是windows server 2008)
2、py程序部署在windows服务器,后台运行,基于bat脚本启停程序
Windows server 2008 FTP环境配置
1、安装FTP服务
开始 --》管理工具 --》服务器管理器
2、安装IIS/FTP角色
打开服务器管理器,找到添加角色,然后点击,弹出添加角色对话框,选择下一步
下一步
选择Web服务器(IIS),然后选择FTP服务,直到安装完成。
参考:http://www.cnblogs.com/Denny_Yang/p/3741041.html
FTP代码
1 class Windows_ftp(object): 2 ''' 3 FTP类,基于ftplib模块实现 4 connect: 连接 5 login: 登陆 6 DownLoadFile: 下载文件 7 DownLoadFileTree: 下载指定目录下的所有文件和目录 8 UpLoadFile: 上传文件 9 UpLoadFileTree: 上传指定目录下的所有文件和目录 10 isDir: 判断是否为目录 11 cwd: 变更目录 12 quit: 退出 13 ''' 14 ftp = ftplib.FTP() 15 bIsDir = False 16 path = "" 17 18 def __init__(self, host, port, username, password): 19 self.host = host 20 self.port = port 21 self.username = username 22 self.password = password 23 self.buffer = 4096 # 设置的缓冲区大小 24 self.ftp.set_debuglevel(0) # 打开调试级别2,显示详细信息; 级别0关闭调试模式 25 26 def connect(self): 27 try: 28 self.ftp.connect(self.host, self.port, timeout=10) 29 logger.info('*** connected to host "%s"' % self.host) 30 return True 31 except Exception as err: 32 logger.error('cannot reach "%s", %s' % (self.host, err)) 33 return False 34 35 def login(self): 36 try: 37 self.ftp.login(self.username, self.password) 38 logger.info('*** Login successfully "%s"' % self.username) 39 return True 40 except Exception as err: 41 logger.error('cannot login, %s' % err) 42 self.quit() 43 return False 44 45 def DownLoadFile(self, LocalFile, RemoteFile): 46 file_handler = open(LocalFile, 'wb') 47 self.ftp.retrbinary("RETR %s" % RemoteFile, file_handler.write, self.buffer) 48 file_handler.close() 49 return True 50 51 def show(self, list): 52 result = list.split(" ") 53 #logger.debug(result) 54 if self.path in result and "" in result: 55 self.bIsDir = True 56 57 def isDir(self, path): 58 self.bIsDir = False 59 self.path = path 60 #this ues callback function ,that will change bIsDir value 61 self.ftp.retrlines('LIST', self.show) 62 return self.bIsDir 63 64 def DownLoadFileTree(self, LocalDir, RemoteDir): 65 if os.path.isdir(LocalDir) == False: # 判断本地主机是否存在目录,进行创建 66 os.makedirs(LocalDir) 67 try: 68 self.cwd(RemoteDir) 69 except Exception as err: 70 logger.error("Failed to open the path to the remote host, %s" % err) 71 return False 72 RemoteNames = self.ftp.nlst() # 列出远程下载目录下所有内容 73 logger.debug("Remote downLoad path:%s, DownLoad files list:%s" % (RemoteDir, RemoteNames)) 74 for file_or_path in RemoteNames: 75 Local = os.path.join(LocalDir, file_or_path) 76 if self.isDir(file_or_path): 77 self.DownLoadFileTree(Local, file_or_path) 78 else: 79 self.DownLoadFile(Local, file_or_path) 80 self.ftp.cwd("..") 81 return 82 83 def cwd(self, DIRN): 84 try: 85 self.ftp.cwd(DIRN) 86 except Exception as err: 87 logger.error('ERROR:cannot CD to "%s"' % DIRN) 88 logger.error(err) 89 self.quit() 90 logger.debug('*** changed to "%s" folder' % DIRN) 91 92 def UpLoadFile(self, LocalFile, RemoteFile): 93 if os.path.isfile(LocalFile) == False: 94 return False 95 file_handler = open(LocalFile, "rb") 96 self.ftp.storbinary('STOR %s' % RemoteFile, file_handler, self.buffer) 97 file_handler.close() 98 return True 99 100 def UpLoadFileTree(self, LocalDir, RemoteDir): 101 if os.path.isdir(LocalDir) == False: 102 return False 103 LocalNames = os.listdir(LocalDir) 104 logger.debug("Remote upLoad path:%s, UpLoad files list:%s" % (RemoteDir, LocalDir)) 105 self.cwd(RemoteDir) 106 for Local in LocalNames: 107 src = os.path.join(LocalDir, Local) 108 if os.path.isdir(src): 109 self.UpLoadFileTree(src, Local) 110 else: 111 self.UpLoadFile(src, Local) 112 self.ftp.cwd("..") 113 return 114 115 def quit(self): 116 self.ftp.quit()
参考:http://www.sharejs.com/codes/python/5619
windows脚本
start.bat
1 @echo off 2 if exist ./var/hk_win_syncfile.pid (echo "[%date% %time%] Running.." 3 ping -n 3 localhost >nul 4 exit 5 )else ( echo "[%date% %time%] Starting.." 6 start pythonw hk_win_syncfile.py 7 ping -n 3 localhost >nul 8 status.bat 9 ping -n 3 localhost >nul 10 )
stop.bat
1 @echo off 2 3 if exist ./var/hk_win_syncfile.pid (echo "[%date% %time%] Stopping..." 4 python -c "import os; os.system('taskkill /F /PID %%s' %% open('./var/hk_win_syncfile.pid').read());" 5 del /s hk_win_syncfile.pid 6 ping -n 3 localhost >nul 7 )else (echo "[%date% %time%] Stopped.." 8 ping -n 3 localhost >nul 9 )
status.bat
1 @echo off 2 if exist ./var/hk_win_syncfile.pid (echo "[%date% %time%] Runningg...")else (echo "[%date% %time%] Stopped..") 3 ping -n 3 localhost >nul
restart.bat
1 @echo off 2 if exist ./var/hk_win_syncfile.pid (echo "[%date% %time%] Stopping..." 3 python -c "import os; os.system('taskkill /F /PID %%s' %% open('./var/hk_win_syncfile.pid').read());" 4 del /s hk_win_syncfile.pid 5 ping -n 3 localhost >nul 6 )else (echo "[%date% %time%] Stopped.." 7 ping -n 3 localhost >nul 8 ) 9 10 if exist ./var/hk_win_syncfile.pid (echo "[%date% %time%] Running.." 11 ping -n 3 localhost >nul 12 exit 13 )else ( echo "[%date% %time%] Starting.." 14 start pythonw hk_win_syncfile.py 15 ping -n 3 localhost >nul 16 status.bat 17 ping -n 3 localhost >nul 18 )
参考:http://my.oschina.net/sanpeterguo/blog/337263