Python实现文件拷贝
2017年8月27日
1.实现目的
统一时间对服务器某文件夹内文件进行备份保存,如若备份成功则不提示任何错误,否则将以邮件的形式告知管理员,备份出错。
2.程序流程图
主要流程图
拷贝流程图
3.代码编写
1.MyMain函数
1 [root@Python CheckCopyFiles]# cat MyMain.py 2 #!/usr/bin/env python 3 4 import os 5 import sys 6 import MyFileZip 7 8 def main() : 9 dir1 = "/root/dir1"10 dir2 = "/root/dir2"11 12 if os.path.isdir(dir2) == False :13 os.mkdir(dir2)14 15 if os.path.isdir(dir1) :16 MyFileZip.FileCopy(dir1,dir2)17 else :18 sys.exit(-1) 19 20 if __name__ == "__main__" :21 main()22 [root@Python CheckCopyFiles]#
2.MyFile函数
1 [root@Python CheckCopyFiles]# cat MyFileZip.py 2 #!/usr/bin/env python 3 4 import zipfile 5 import time 6 import os 7 import MySendMail 8 9 10 def FileCopy(dir1,dir2) :11 try:12 NowDate = time.strftime("%Y-%m-%d")13 Zfile = dir2+ '/'+'logfile_'+NowDate+'.zip'14 zf = zipfile.ZipFile(Zfile,'w')15 16 for filename in os.listdir(dir1) :17 if os.path.isfile(dir1 + '/' + filename) :18 zf.write(dir1 + '/' + filename)19 os.remove(dir1 + '/' + filename)20 21 zf.close()22 except:23 MySendMail.SamMail(NowDate,dir1,dir2)24 [root@Python CheckCopyFiles]#
3.SendMail函数
1 [root@Python CheckCopyFiles]# cat MySendMail.py 2 #!/usr/bin/env python 3 4 import smtplib 5 import email.mime.text 6 7 def SamMail(NowDate,dir1,dir2) : 8 HOST = "smtp.163.com" 9 SUBJECT = "Copy File Warning"10 TO = "发送的帐号"11 FROM = "来自于账户"12 Remask = "Copy dirctory warning"13 14 msg = email.mime.text.MIMEText("""15 16 17Warning
18Time:%s
19Dirctory:%s
20Remask:%s
21 22 23 """ % (NowDate,dir1,"Copy File Error"),"html","utf-8")24 25 msg['Subject'] = SUBJECT26 msg['From'] = FROM27 msg['TO'] = TO28 29 try:30 server = smtplib.SMTP()31 server.connect(HOST,'25')32 server.starttls()33 server.login("帐号","密码")34 server.sendmail(FROM,TO,msg.as_string())35 server.quit()36 except:37 print ("Send mail Error")38 [root@Python CheckCopyFiles]#
4.实现效果
4.1 建立新的文件
1 [root@Python ~]# touch /root/dir1/7 /root/dir1/82 [root@Python ~]#
4.2 执行代码
1 [root@Python ~]# python /root/python/CheckCopyFiles/MyMain.py 2 [root@Python ~]#
4.3 查看文件
1 [root@Python ~]# ls -l /root/dir1 -a2 total 03 drwxrwxrwx. 2 root root 6 Aug 27 09:26 .4 dr-xr-x---. 6 root root 201 Aug 27 09:26 ..5 [root@Python ~]#
1 [root@Python ~]# ls -l /root/dir2/ 2 total 4 3 -rw-r--r--. 1 root root 218 Aug 27 09:28 logfile_2017-08-27.zip 4 [root@Python ~]# unzip -v /root/dir2/logfile_2017-08-27.zip 5 Archive: /root/dir2/logfile_2017-08-27.zip 6 Length Method Size Cmpr Date Time CRC-32 Name 7 -------- ------ ------- ---- ---------- ----- -------- ---- 8 0 Stored 0 0% 08-27-2017 09:27 00000000 root/dir1/7 9 0 Stored 0 0% 08-27-2017 09:27 00000000 root/dir1/810 -------- ------- --- -------11 0 0 0% 2 files12 [root@Python ~]#
由此可见,dir1中的文件已经备份至/root/dir2/logfile_2017-08-27.zip中,且已经删除了原文件
4.3 查看发送邮件效果
修改一下源码:
1 [root@Python CheckCopyFiles]# cat MyFileZip.py 2 #!/usr/bin/env python 3 4 import zipfile 5 import time 6 import os 7 import MySendMail 8 9 10 def FileCopy(dir1,dir2) :11 try:12 NowDate = time.strftime("%Y-%m-%d")13 Zfile = dir2+ '/'+'logfile_'+NowDate+'.zip'14 zf = zipfile.ZipFile(Zfile,'w')15 16 for filename in os.listdir(dir1) :17 if os.path.isfile(dir1 + '/' + filename) :18 zf.write(dir1 + '/' + filename)19 os.remove(dir1 + '/' + filename)20 21 zf.close()22 sys.exit()23 except:24 MySendMail.SamMail(NowDate,dir1,dir2)25 [root@Python CheckCopyFiles]#
22行 sys 模块并未导入,故一定会出错
执行效果如下:
1 [root@Python ~]# python /root/python/CheckCopyFiles/MyMain.py 2 [root@Python ~]#
收到邮件的效果如下:
只要把脚本写入crontab,那样就可以按时执行了,例如:
1 [root@Python CheckCopyFiles]# crontab -l2 00 14 * * 0 /usr/bin/python /root/python/CheckCopyFiles/MyMain.py3 [root@Python CheckCopyFiles]#
意思是每周日14点调用/usr/bin/python 去执行/root/python/CheckCopyFiles/MyMain.py文件
至此,由python写的备份脚本已经全部完成,人生苦短,我用python