博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
对目录数据进行归档及删除实例
阅读量:7103 次
发布时间:2019-06-28

本文共 2961 字,大约阅读时间需要 9 分钟。

对目录数据进行归档及删除实例:

vi a1.py

#!/usr/bin/python

import os

import datetime
import shutil
import sys

reload(sys)

sys.setdefaultencoding('utf-8')

dir="/historybackup2/PRD"

dst="/historybackup2/Daybackup/PRD"

yesterday = datetime.date.today() - datetime.timedelta(days=1)

yesterday2 = datetime.datetime.now() - datetime.timedelta(days=15)
yesterday3 = datetime.datetime.now() - datetime.timedelta(days=45)

os.remove("/historybackup2/shell/logs/PRD.txt")

os.mknod("/historybackup2/shell/logs/PRD.txt")
os.remove("/historybackup2/shell/logs/PRD2.txt")
os.mknod("/historybackup2/shell/logs/PRD2.txt")
os.remove("/historybackup2/shell/logs/PRD_2.txt")
os.mknod("/historybackup2/shell/logs/PRD_2.txt")
os.remove("/historybackup2/shell/logs/PRD2_2.txt")
os.mknod("/historybackup2/shell/logs/PRD2_2.txt")

os.chdir(dst)

os.mkdir(str(yesterday))

os.chdir(dir)

for root,dirs,files in os.walk("."):

for f in files:
mtime = datetime.date.fromtimestamp(os.path.getmtime(os.path.join(root,f)))
if mtime == yesterday:
z = open('/historybackup2/shell/logs/PRD2.txt','a')
z.write(str(mtime)+"\n")
z.close()

p = open('/historybackup2/shell/logs/PRD.txt','a')                 p.write(os.path.join(root,f)+"\n")                 p.close()

size = 0

c = open('/historybackup2/shell/logs/PRDBACKUP.txt','r')
for i in c.readlines():
b = os.path.getsize(i.strip())
size += int(b)
si = (format((float(size) / 1024 / 1024 / 1024),'.1f'))
e = open('/historybackup2/shell/logs/PRD3.txt','w')
e.write(str(si)+'G')
e.close()

x = open('/historybackup2/shell/logs/PRD.txt','r')

for n in x.readlines():
m = os.path.dirname(n.strip()).strip('./')
s = os.path.join(dst,str(yesterday))
if not os.path.exists(os.path.join(s,m)):
os.makedirs(os.path.join(s,m))
shutil.copy2(n.strip(),os.path.join(s,n.strip()))

for f2 in os.listdir(dir):

mtime = datetime.datetime.fromtimestamp(os.path.getmtime(os.path.join(dir,f2)))
if mtime < yesterday2:
z2 = open('/historybackup2/shell/logs/PRD2_2.txt','a')
z2.write(str(mtime)+"\n")
z2.close()

p2 = open('/historybackup2/shell/logs/PRD_2.txt','a')                 p2.write(f2+"\n")                 p2.close()

x2 = open('/historybackup2/shell/logs/PRD_2.txt','r')

for n2 in x2.readlines():
if os.path.isfile(n2.strip()):
os.remove(n2.strip())
if os.path.isdir(n2.strip()):
shutil.rmtree(n2.strip())

os.chdir(dst)

for f3 in os.listdir(dst):

if os.path.isdir(f3):
mtime3 = datetime.datetime.fromtimestamp(os.path.getmtime(os.path.join(dst,f3)))
if mtime3 < yesterday3 and f3 != "2018-01-01" and f3 != "2018-02-01" and f3 != "2018-03-01" and f3 != "2018-04-01" and f3 != "2018-05-01" and f3 != "2018-06-01" and f3 != "2018-07-01" and f3 != "2018-08-01" and f3 != "2018-09-01" and f3 != "2018-10-01" and f3 != "2018-11-01" and f3 != "2018-12-01":
shutil.rmtree(f3)

os.system('/usr/bin/mail -s "host1(10.0.0.2) aa archive yesterday Size at date +%Y-%m-%d" zhi.yang@ming.com < /historybackup2/shell/logs/PRD3.txt')

:wq

python a1.py

转载于:https://blog.51cto.com/yangzhiming/2135172

你可能感兴趣的文章
js正则实现用户输入银行卡号的控制及格式化
查看>>
MySQL
查看>>
2018-08-14期 Zookeeper客户端连接工具ZooInspector使用方法
查看>>
unity3D初识对象池技术
查看>>
emulator-arm.exe停止工作
查看>>
spring用动态代理还是cglib?
查看>>
共同抵制恶意APP CNCERT公布首批黑名单
查看>>
STP工作过程分析<欢迎指正>
查看>>
您是在为这些问题苦恼吗?
查看>>
几种http-equiv
查看>>
gnuplot绘制曲线图
查看>>
静态缓存
查看>>
使用mdadm创建软raid
查看>>
网络传输之数据单位 kbpsKB/s
查看>>
jquey写的简单图片轮转
查看>>
《JavaScript权威指南》代码解读 -- 第9章:类和模块 (导论)
查看>>
字符测试 =~ 用法
查看>>
【OSChina-MoPaaS应用开发大赛】BiLi记事本
查看>>
Java: String, StringBuilder和StringBuffer 三者之间的区别
查看>>
在linux下安装配置svn独立服务器
查看>>