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

python备份网站,并删除指定日期文件的更多相关文章

  1. 定时备份为Sharepoint做网站备份,并删除指定日期的备份

    一.创建bat文件 @echo cd \ c: cd "Program Files\Common Files\Microsoft Shared\web server extensions\1 ...

  2. Debian下自动备份文件并上传到远程FTP服务器且删除指定日期前的备份Shell脚本

    说明:  1.备份目录/home/osyunwei下面所有的文件到/home/osyunweibak里面,并且保存为osyunwei20120701.tar.gz的压缩文件格式(2012_07_01是 ...

  3. 利用任务计划自动删除指定日期的SQLServer备份文件

    利用任务计划自动删除指定日期的SQLServer备份文件 命令FORFILES [/P pathname] [/M searchmask] [/S]         [/C command] [/D ...

  4. Linux备份-删除指定日期内文件

    #!/usr/bin/env bash source /etc/profile echo " *************** start filter ***************  &q ...

  5. CentOS Linux自动备份MySQL数据库到远程FTP服务器并删除指定日期前的备份Shell脚本

    说明: 我这里要把MySQL数据库存放目录/var/lib/mysql下面的pw85数据库备份到/home/mysql_data里面,并且保存为mysqldata_bak_2011_11_03.tar ...

  6. 【Linux】linux中删除指定日期之前的文件

    要删除系统中就的备份文件,就需要使用命令了: #find /tmp -mtime +30 -type f -name *.sh[ab] -exec rm -f {} \; 假如在一个目录中保留最近30 ...

  7. Mongodb自动备份数据库并删除指定天数前的备份

    1.创建Mongodb数据库备份目录 mkdir -p /home/backup/mongod_bak/mongod_bak_now mkdir -p /home/backup/mongod_bak/ ...

  8. outlook寻找/删除指定日期范围内的邮件

    总是收到很多系统预警邮件,时间久了攒了好多垃圾邮件.实际上只需保存近期预警邮件,之前的完全可以删除. 上网找了一圈也没找到方法,然后自己想到了一种,步骤如下: 使用outlook规则,将指定日期范围内 ...

  9. Inno如何在安装完成时删除指定的文件夹(下的所有文件及子目录)??

    删除安装目录下的任意文件夹及下的所有文件及子目录,或者删除指定目录的文件夹,要如何做到呢?谢谢!! //删除文件    用 DeleteFile 只能删除一个文件,不能使用通配符来删除多个文件Dele ...

随机推荐

  1. 过滤器Filter的使用(以登录为例子)

    使用过滤器步骤: (1)在web.xml文件中添加过滤器(以下例子是过滤多个请求) <!-- 用户登录过滤 --> <filter> <filter-name>lo ...

  2. eclipse插件wordwrap

    一行代码很长,浏览不方便,安装wordwrap可以自动折行. help->install new software-,在Workwith输入wordwrap - http://ahtik.com ...

  3. PAT (Basic Level) Practice 1001 害死人不偿命的(3n+1)猜想

    https://pintia.cn/problem-sets/994805260223102976/problems/994805325918486528 卡拉兹(Callatz)猜想: 对任何一个自 ...

  4. HDU 1236 排名(Microsoft_zzt)

    http://acm.hdu.edu.cn/showproblem.php?pid=1236 Problem Description 今天的上机考试虽然有实时的Ranklist,但上面的排名只是根据完 ...

  5. Eclipse使用Maven2的一次环境清理记录

    1. C:\Users\Administrator\.m2\repository\com\yuanchuangyun\[module,yuanchuangyun-*]相关目录全删除.2. D:\wor ...

  6. vCenter 异常关机后无法开启ESXi虚拟机的处理.

    春节假期一个插排掉电了, 然后vcenter不幸自动关机了. 对虚拟机进行开机时错误提示 前期更改过administrator的密码, 但是忘记同步修改sqlserver启动时的用户了(这里使用adm ...

  7. 软件工程_5th weeks

    从周五开始经历了清明小长假,周六.周日和周一都处于假期状态,因此对于上篇博客的评论回复的很迟,而这周的工作做的也不多. 1.爆打小组 4.5日站立会议 时间:2016.4.5 15:00-15:34 ...

  8. gitbook 简单使用

    gitbook 简单使用 之前由于公司培训过使用 gitbook,当时也没有进行安装使用.当时就感觉就是 markdown 语法,加上我本来就是使用 markdown 进行写博客之类.再加上这个东西本 ...

  9. js break和continue

    break的兩種形式: break:未加標籤,適用於switch和循環結構 break labelname:適用於任何代碼塊: continue的兩種形式: continue:適用於循環結構 cont ...

  10. python----内置函数2与匿名函数

    1.迭代器生成器相关 range:创建一个可迭代对象,一般与for混合使用,可设置步长. for i in range(0,10,2): #步长2范围为0-10不包括10 print(i) # 0 2 ...