#!/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 "\n>>>>>>>>>>>>>>\n"
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)

最新文章

  1. Eclipse 导入外部项目无法识别为web项目并且无法在部署到tomcat下
  2. JavaScript 学习小结
  3. PourOver – 快速筛选和排序大的数据集合
  4. java list 交集 并集 差集 去重复并集
  5. Spring AOP 完成日志记录
  6. phpexcel相关函数
  7. C++ Primer 第三章 标准库类型string运算
  8. ###STL学习--关联容器
  9. uva 165 Stamps
  10. jQuery学习 day01
  11. MySQL具体解释(7)-----------MySQL线程池总结(一)
  12. 【转】Android 4.0.3 CTS 测试
  13. 关于select元素的一些基本知识
  14. 8、Khala的设备间管理+通信
  15. 【Mysql5.7数据目录和配置文件目录】
  16. android Service Activity三种交互方式(付源码)(转)
  17. 一个关于Python正则表达式的快速使用手册
  18. 分针网—每日分享: 怎么轻松学习JavaScript
  19. bootstrap table 和 x-editable 使用方法
  20. Oracle 11g 的 自动内存管理

热门文章

  1. MySQL 单表优化
  2. squid反向代理
  3. git学习笔记2——ProGit2
  4. Trouble shooting(问题解决):centos 7 gnome show someting has gone wrong.
  5. Vue的filter属性
  6. Docker(十四)-Docker四种网络模式
  7. [转帖]Lifetime Support Stages for Your Oracle Products
  8. 当返回值为json字符串时 如何获得其中的json数组
  9. Angular生成二维码
  10. hive 远程管理