描述:
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代码
class Windows_ftp(object):
'''
FTP类,基于ftplib模块实现
connect: 连接
login: 登陆
DownLoadFile: 下载文件
DownLoadFileTree: 下载指定目录下的所有文件和目录
UpLoadFile: 上传文件
UpLoadFileTree: 上传指定目录下的所有文件和目录
isDir: 判断是否为目录
cwd: 变更目录
quit: 退出
'''
ftp = ftplib.FTP()
bIsDir = False
path = "" def __init__(self, host, port, username, password):
self.host = host
self.port = port
self.username = username
self.password = password
self.buffer = 4096 # 设置的缓冲区大小
self.ftp.set_debuglevel(0) # 打开调试级别2,显示详细信息; 级别0关闭调试模式 def connect(self):
try:
self.ftp.connect(self.host, self.port, timeout=10)
logger.info('*** connected to host "%s"' % self.host)
return True
except Exception as err:
logger.error('cannot reach "%s", %s' % (self.host, err))
return False def login(self):
try:
self.ftp.login(self.username, self.password)
logger.info('*** Login successfully "%s"' % self.username)
return True
except Exception as err:
logger.error('cannot login, %s' % err)
self.quit()
return False def DownLoadFile(self, LocalFile, RemoteFile):
file_handler = open(LocalFile, 'wb')
self.ftp.retrbinary("RETR %s" % RemoteFile, file_handler.write, self.buffer)
file_handler.close()
return True def show(self, list):
result = list.split(" ")
#logger.debug(result)
if self.path in result and "<DIR>" in result:
self.bIsDir = True def isDir(self, path):
self.bIsDir = False
self.path = path
#this ues callback function ,that will change bIsDir value
self.ftp.retrlines('LIST', self.show)
return self.bIsDir def DownLoadFileTree(self, LocalDir, RemoteDir):
if os.path.isdir(LocalDir) == False: # 判断本地主机是否存在目录,进行创建
os.makedirs(LocalDir)
try:
self.cwd(RemoteDir)
except Exception as err:
logger.error("Failed to open the path to the remote host, %s" % err)
return False
RemoteNames = self.ftp.nlst() # 列出远程下载目录下所有内容
logger.debug("Remote downLoad path:%s, DownLoad files list:%s" % (RemoteDir, RemoteNames))
for file_or_path in RemoteNames:
Local = os.path.join(LocalDir, file_or_path)
if self.isDir(file_or_path):
self.DownLoadFileTree(Local, file_or_path)
else:
self.DownLoadFile(Local, file_or_path)
self.ftp.cwd("..")
return def cwd(self, DIRN):
try:
self.ftp.cwd(DIRN)
except Exception as err:
logger.error('ERROR:cannot CD to "%s"' % DIRN)
logger.error(err)
self.quit()
logger.debug('*** changed to "%s" folder' % DIRN) def UpLoadFile(self, LocalFile, RemoteFile):
if os.path.isfile(LocalFile) == False:
return False
file_handler = open(LocalFile, "rb")
self.ftp.storbinary('STOR %s' % RemoteFile, file_handler, self.buffer)
file_handler.close()
return True def UpLoadFileTree(self, LocalDir, RemoteDir):
if os.path.isdir(LocalDir) == False:
return False
LocalNames = os.listdir(LocalDir)
logger.debug("Remote upLoad path:%s, UpLoad files list:%s" % (RemoteDir, LocalDir))
self.cwd(RemoteDir)
for Local in LocalNames:
src = os.path.join(LocalDir, Local)
if os.path.isdir(src):
self.UpLoadFileTree(src, Local)
else:
self.UpLoadFile(src, Local)
self.ftp.cwd("..")
return def quit(self):
self.ftp.quit()
参考:http://www.sharejs.com/codes/python/5619
windows脚本
start.bat
@echo off
if exist ./var/hk_win_syncfile.pid (echo "[%date% %time%] Running.."
ping -n 3 localhost >nul
exit
)else ( echo "[%date% %time%] Starting.."
start pythonw hk_win_syncfile.py
ping -n 3 localhost >nul
status.bat
ping -n 3 localhost >nul
)
stop.bat
@echo off if exist ./var/hk_win_syncfile.pid (echo "[%date% %time%] Stopping..."
python -c "import os; os.system('taskkill /F /PID %%s' %% open('./var/hk_win_syncfile.pid').read());"
del /s hk_win_syncfile.pid
ping -n 3 localhost >nul
)else (echo "[%date% %time%] Stopped.."
ping -n 3 localhost >nul
)
status.bat
@echo off
if exist ./var/hk_win_syncfile.pid (echo "[%date% %time%] Runningg...")else (echo "[%date% %time%] Stopped..")
ping -n 3 localhost >nul
restart.bat
@echo off
if exist ./var/hk_win_syncfile.pid (echo "[%date% %time%] Stopping..."
python -c "import os; os.system('taskkill /F /PID %%s' %% open('./var/hk_win_syncfile.pid').read());"
del /s hk_win_syncfile.pid
ping -n 3 localhost >nul
)else (echo "[%date% %time%] Stopped.."
ping -n 3 localhost >nul
) if exist ./var/hk_win_syncfile.pid (echo "[%date% %time%] Running.."
ping -n 3 localhost >nul
exit
)else ( echo "[%date% %time%] Starting.."
start pythonw hk_win_syncfile.py
ping -n 3 localhost >nul
status.bat
ping -n 3 localhost >nul
)