这篇文章主要为大家详细介绍了python备份远程FTP脚本实现方法,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随四海网的小编罗X来看看吧。
# 来自www.q1010.com
#!/usr/bin/python
# -*- coding: utf-8 -*-
import commands
import sys
import os
import re
import zipfile
from datetime import date,datetime,timedelta
class Package:
"""
检查fuse-curlftpfs软件包的安装与否,如果没有,则安装之
"""
def __init__(self,name):
self.name=name
def install(self):
print('execute "yum install ' + self.name + '"...')
#cmd=commands.getstatusoutput('yum install ' + self.name + ' -y')
print os.system('yum install ' + self.name + ' -y')
if self.name in os.popen('rpm -qa | grep ' + self.name).read():
print('Install ' + self.name + ' successful.')
else:
print('Install ' + self.name + ' failed, aborting.')
sys.exit()
def checkinstall(self):
cmd=commands.getstatusoutput('rpm -qa | grep ' + self.name)
if self.name in cmd[1]:
print(self.name + ' has aleady installed. Continue...')
else:
print(self.name + ' isn\'t installed. Ready to install it.')
self.install()
class Backup:
"""
使用shell命令挂载之,然后执行备份过程(压缩目录,删除旧文件),压缩完成以后再解除挂载
"""
def __init__(self,server,user,passwd,backupname):
self.server=server
self.user=user
self.passwd=passwd
self.backupname=backupname
def mount(self):
try:
if os.path.isdir('/' + self.backupname):
print('found mount point /' + self.backupname + ', ready to mount ftp server.')
else:
print('doesn\'t found mount point /' + self.backupname + ', build it.')
os.mkdir('/' + self.backupname)
except IOError, error:
print error
sys.exit()
cmd=commands.getstatusoutput(r'curlftpfs ftp://' + self.user + ':' + self.passwd + '@' + self.server + r' /' + self.backupname + ' -o codepage=utf8')
print cmd[1]
pat=r'fuse(.+)/' + self.backupname
all=os.popen('df -hT').read()
if re.search(pat,all):
print('mounted /' + self.backupname + ', continue...')
else:
print('mount failed. please check.')
sys.exit()
def backup(self):
newday = date.today() #获取今天的日期
oldday = date.today()-timedelta(5) #获得5天前的日期
newfile = self.backupname + '_backup_' + str(newday.year) + '.' + str(newday.month) + '.' + str(newday.day) + '.zip' #本次备份的文件名
oldfile = self.backupname + '_backup_' + str(oldday.year) + '.' + str(oldday.month) + '.' + str(oldday.day) + '.zip' #5天前备份的文件名
print('delete old file...')
try:
if os.path.isfile(oldfile):
os.remove(oldfile)
else:
print('don\'t found old file, jumped.')
except IOError, err:
print err
print('compress directory. it will take a while.')
try:
f = zipfile.ZipFile(newfile,'w',zipfile.ZIP_DEFLATED)
for dirpath, dirnames, filenames in os.walk(r'/' + self.backupname):
for filename in filenames:
f.write(os.path.join(dirpath,filename))
f.close()
print("backup completely! file name is " + newfile)
except (IOError, OSError), err:
print err
self.umount()
sys.exit()
def umount(self):
cmd=commands.getstatusoutput('umount /' + self.backupname)
pat=r'fuse(.+)/' + self.backupname
all=os.popen('df -hT').read()
if not re.search(pat,all):
print('umounted /' + self.backupname + '. Done!')
else:
print('umount failed. please check.')
if __name__=='__main__':
ps=Package('fuse-curlftpfs')
ps.checkinstall()
#标示符是挂载到本的的文件名,及压缩以后的文件名,只能用英文且不能有空格,可以用下划线
#如果要备份多个FTP,把下面4行复制一遍即可,注意每个的标示符不能一样
bu=Backup('FTP地址','用户名','密码','标示符')
bu.mount()
bu.backup()
bu.umount()
本文来自:http://www.q1010.com/181/2144-0.html
注:关于python备份远程FTP脚本实现方法的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:脚本
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。