Python backup, delete expired compressed files

Source: Internet
Author: User

#!/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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.