#!/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 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'