zoukankan      html  css  js  c++  java
  • python备份网站,并删除指定日期文件

    #!/usr/bin/python
    # Filename: backup_ver1.py
    import os
    import time
    import datetime
    # 1. The files and directories to be backed up are specified in a list.
    source = ['/software/tengine/html/mtax/sbzs','/software/tengine/html/mtax/static']
    # If you are using Windows, use source = [r'C:Documents', r'D:Work'] or something like that
    # 2. The backup must be stored in a main backup directory
    target_dir = '/software/tengine/html/mtax/backup/' # Remember to change this to what you will be using
    # 3. The files are backed up into a zip file.
    # 4. The name of the zip archive is the current date and time
    target = target_dir + time.strftime('%Y%m%d%H%M%S') + '.zip'
    # 5. We use the zip command (in Unix/Linux) to put the files in a zip archive
    zip_command = "zip -qr '%s' %s" % (target, ' '.join(source))
    # Run the backup
    if os.system(zip_command) == 0:
    print 'Successful backup to', target
    else:
    print 'Backup FAILED'
    def should_remove(path, pattern, days):
    if not path.endswith(pattern):
    return False

    mtime = os.path.getmtime(path)
    now = time.time()
    result = now - mtime > days * 24 * 3600

    print " >>>>>>>>>>>>>> "
    print "file: ", path
    print "mtime: ", datetime.datetime.fromtimestamp(mtime)
    print "now: ", datetime.datetime.fromtimestamp(now)
    print "> %d days: " % days, result

    return result


    def findNremove(path, pattern, days):
    print "path: ", path
    print "pattern: ", pattern
    print "days: ", days

    for r, d, f in os.walk(path):
    for files in f:
    file_path = os.path.join(r, files)
    if should_remove(file_path, pattern, days):
    try:
    print "Removing %s" % (file_path)
    os.remove(file_path)
    except Exception, e:
    print e
    else:
    print "removed %s" % (file_path)


    if __name__ == '__main__':
    path = os.path.join("/software/tengine/html/mtax/backup/")
    days = 30
    pattern = ".zip"
    findNremove(path, pattern, days)

  • 相关阅读:
    SASS常用方法
    Vue 全局过滤和局部过滤
    Java进阶知识点8:高可扩展架构的利器
    InnoDB MVCC RR隔离级别下的数据可见性总结
    记一次诡异的网络故障排除
    数据库的读锁和写锁在业务上的应用场景总结
    Gradle配置IDEA正常识别JPA Metamodel Generator动态生成的代码
    程序员容易读错的单词
    Java进阶知识点7:不要只会写synchronized
    Java进阶知识点6:并发容器背后的设计理念
  • 原文地址:https://www.cnblogs.com/cheyunhua/p/8064000.html
Copyright © 2011-2022 走看看