#!/usr/bin/python
#-*-Coding:utf-8-*-
Import Os,sys,datetime
Import Shutil
Import time
def zipfile (s_dir,path= "."):
target = path + os.sep + s_dir.split ('/') [-1] + time.strftime ('%y%m%d%h%m%s ') + '. Zip '
Zip_command = "Zip-qr%s%s-x ' *.gz '"% (Target,s_dir)
If Os.system (zip_command) = = 0:
print ' successful backup to ', target
Else
print ' Backup failed! '
def removefilesbeforedate (beforetime, Path = "."):
For Eachfile in Os.listdir (path):
F = path + Os.sep + eachfile
Lastmtime = Os.stat (f). St_mtime
If Lastmtime <= beforetime:
Try
If Os.path.isfile (f):
Os.remove (f)
Elif Os.path.isdir (f):
Shutil.rmtree (f)
Else
Os.remove (f)
Print ("Delete {0}, Success!") ". Format (Eachfile))
Except Exception as E:
Print ("Delete {0}, failed!") The error is as follows: ". Format (Eachfile))
Print (e)
if __name__ = = ' __main__ ':
Currtime = Time.time ()
Delttime = 3600*24*7
Befortime = Currtime-delttime
Path = "/app/sinova/back"
S_dir = "/app/sinova/nginx_node2"
ZipFile (S_dir,path)
Removefilesbeforedate (befortime, Path)
This article is from the "Python" blog, so be sure to keep this source http://patrick0715.blog.51cto.com/3681259/1810415
Python backup, delete expired compressed files