CentOS环境使用python脚本备份文件

前端之家收集整理的这篇文章主要介绍了CentOS环境使用python脚本备份文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

备份脚本一

#!/usr/bin/python
#Filename:backup_ver1.py

importos
importtime

#1.Thefilesanddirectoriestobebackeduparespecifiedinalist.
source=['/home/swaroop/byte','/home/swaroop/bin']
#IfyouareusingWindows,usesource=[r'C:\Documents',r'D:\Work']orsomethinglikethat

#2.Thebackupmustbestoredinamainbackupdirectory
target_dir='/mnt/e/backup/'#Remembertochangethistowhatyouwillbeusing

#3.Thefilesarebackedupintoazipfile.
#4.Thenameoftheziparchiveisthecurrentdateandtime
target=target_dir+time.strftime('%Y%m%d%H%M%S')+'.zip'

#5.Weusethezipcommand(inUnix/Linux)toputthefilesinaziparchive
zip_command="zip-qr'%s'%s"%(target,''.join(source))

#Runthebackup
ifos.system(zip_command)==0:
print'Successfulbackupto',target
else:
print'BackupFailed'

备份脚本二

#!/usr/bin/python
#Filename:backup_ver2.py

importos
importtime

#1.Thefilesanddirectoriestobebackeduparespecifiedinalist.
source=['/home/swaroop/byte',r'D:\Work']orsomethinglikethat

#2.Thebackupmustbestoredinamainbackupdirectory
target_dir='/mnt/e/backup/'#Remembertochangethistowhatyouwillbeusing

#3.Thefilesarebackedupintoazipfile.
#4.Thecurrentdayisthenameofthesubdirectoryinthemaindirectory
today=target_dir+time.strftime('%Y%m%d')
#Thecurrenttimeisthenameoftheziparchive
now=time.strftime('%H%M%S')

#Createthesubdirectoryifitisn'talreadythere
ifnotos.path.exists(today):
os.mkdir(today)#makedirectory
print'Successfullycreateddirectory',today

#Thenameofthezipfile
target=today+os.sep+now+'.zip'

#5.Weusethezipcommand(inUnix/Linux)toputthefilesinaziparchive
zip_command="zip-qr'%s'%s"%(target,255);">备份脚本三 
#!/usr/bin/python
#Filename:backup_ver3.py

importos
importtime

#1.Thefilesanddirectoriestobebackeduparespecifiedinalist.
source=['/home/swaroop/byte',r'D:\Work']orsomethinglikethat

#2.Thebackupmustbestoredinamainbackupdirectory
target_dir='/mnt/e/backup/'#Remembertochangethistowhatyouwillbeusing

#3.Thefilesarebackedupintoazipfile.
#4.Thecurrentdayisthenameofthesubdirectoryinthemaindirectory
today=target_dir+time.strftime('%Y%m%d')
#Thecurrenttimeisthenameoftheziparchive
now=time.strftime('%H%M%S')

#Takeacommentfromtheusertocreatethenameofthezipfile
comment=raw_input('Enteracomment-->')
iflen(comment)==0:#checkifacommentwasentered
target=today+os.sep+now+'.zip'
else:
target=today+os.sep+now+'_'+\
comment.replace('','_')+'.zip'
#Noticethebackslash!

#Createthesubdirectoryifitisn'talreadythere
ifnotos.path.exists(today):
os.mkdir(today)#makedirectory
print'Successfullycreateddirectory',today

#5.Weusethezipcommand(inUnix/Linux)toputthefilesinaziparchive
zip_command="zip-qr'%s'%s"%(target,target
else:
print'BackupFailed'
原文链接:https://www.f2er.com/centos/375107.html

猜你在找的CentOS相关文章