python备份2

来源:互联网 发布:树莓派ubuntu系统vnc 编辑:程序博客网 时间:2024/05/19 15:39

 

(本文由Tengda huang 发表于 http://blog.csdn.net/cogent2001 ,该文章所提到的程序为原创,使用者可以任意引用,修改该程序。转载请注明出处,谢谢!)

       近来忙东忙西,有些重复性的事务就懒得做,比如文件备份。不过不做也不行。这两天闲下来,现学现用python写了这个文件自动备份的脚本。

        有以下2个亮点:

1.可以放在计划任务中定期执行,所需备份的内容由dsvr1list.txt文件提供,备份文件自动备份到按当前日期生成的目录中。

2.程序刚开始就执行清除1个月以前的所有备份目录,这个功能对于只有特定大小的备份设备及其有用,从此文件备份完全不用人工干涉。

      代码很简单,该注释的我都注释了。需要注意的是,我安装的的是python 2.5.1,是否对其他版本的python兼容有待考查;压缩格式我选用7-zip,其中7z.exe是它的命令行程序,该软件为开源软件,并且压缩比应 该算是同类软件中最高的。(经过我的测试,备份文件服务器上2.4G左右的东西,压缩后只剩不到900M)如果第一次安装python环境和7-zip软 件,请为它们设置path变量,因为我的脚本里面假定它们可以在任何目录下执行。

#!/usr/bin/python
#
Filename: bDatasvr1.py
#
This program is for files backup only
#
It also needs 7-zip as the compress tool.
#
Tengda huang, Dec 17th, 2007
#
ver 1.0

import os
import time
import distutils.dir_util
import datetime

# connecting to the remote computer
link_command = r"net use k: //10.10.10.1/mysvr1 mypassword /user:backupUser"

print 'Connecting to remote computer'

if os.system(link_command) == 0:
    
print 'Successful connecting to drive k !'
else:
    
print 'Drive k already linked or link failed!'

# delete old directories and files if the dir name created by time is older than 30 days
for root, dirs, files in os.walk('k:'):
    
for name in dirs:
         (y1, m1, d1)
= (int(x) for x in name.split('-'))
         date1
= datetime.date(y1, m1, d1)
         datenow
= time.strftime('%Y%m%d')
         y2
= int(datenow[:4])
         m2
= int(datenow[4:6])
         d2
= int(datenow[6:])
         date2
= datetime.date(y2, m2, d2)
        
if (date2 - date1).days > 30:
            
print 'Expired dir! Deleting directory... ', name
             distutils.dir_util.remove_tree(os.path.join(
"k:",name))
print 'Old directory deleting done!'
print 'Starting to create backup files!'

# 1. The files and directories to be backed up are specified in the list.
source = r'@dsvr1list.txt'

# 2. The backup must be stored in a main directory,
#
     that is  //10.10.10.1mysvr1
#
     which mapped as drive k:
target_dir = 'k:'  

# 3. The files are compressed and backed up into a 7-zip file type.
#
     The subdirectories are named by the current day time.
today = target_dir + time.strftime('%Y-%m-%d')

# The current time is the name of the zip archive
now = time.strftime('%H%M%S')

# Create the subdirectory if it isn't already there
if not os.path.exists(today):
     os.mkdir(today)
# make directory
    print 'Successfully created directory', today

# The name of the zip file
target = today + os.sep + 'share' + now + '.7z'

# 5. Use the 7z command to compress and put the files in a 7z archive
zip_command = "7z a -t7z %s %s" % (target, source)

# Runing the backup
if os.system(zip_command) == 0:
    
print 'Successful backup to', target
else:
    
print 'Backup FAILED'

# Disconnect from the remote computer
unlink_command = r"net use k: /delete"

if os.system(unlink_command) == 0:
    
print 'Successfully detach from drive k! '
    
print 'All job done!'
else:
    
print 'Backup FAILED'