(本文由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变量,因为我的脚本里面假定它们可以在任何目录下执行。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/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'