1. 程式人生 > >(windows)python指令碼:自動備份並壓縮檔案,同時刪除過期檔案

(windows)python指令碼:自動備份並壓縮檔案,同時刪除過期檔案

(本文由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, 
#

#
    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
ifnot 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'