python实现备份目录的方法

本文实例讲述了python实现备份目录的方法。分享给大家供大家参考。具体如下:

备份脚本1:

#!/usr/bin/python
# filename: backup_ver1.py
import os
import time
# 1. the files and directories to be backed up are specified in a list.
source = [‘/home/swaroop/byte’, ‘/home/swaroop/bin’]
# if you are using windows, use source = [r’c:\documents’, r’d:\work’] or something like that
# 2. the backup must be stored in a main backup directory
target_dir = ‘/mnt/e/backup/’ # remember to change this to what you will be using
# 3. the files are backed up into a zip file.
# 4. the name of the zip archive is the current date and time
target = target_dir + time.strftime(‘%y%m%d%h%m%s’) + ‘.zip’
# 5. we use the zip command (in unix/linux) to put the files in a zip archive
zip_command = “zip -qr ‘%s’ %s” % (target, ‘ ‘.join(source))
# run the backup
if os.system(zip_command) == 0:
print ‘successful backup to’, target
else:
print ‘backup failed’

输出:

$ python backup_ver1.py
successful backup to /mnt/e/backup/20041208073244.zip

备份脚本2:

#!/usr/bin/python
# filename: backup_ver2.py
import os
import time
# 1. the files and directories to be backed up are specified in a list.
source = [‘/home/swaroop/byte’, ‘/home/swaroop/bin’]
# if you are using windows, use source = [r’c:\documents’, r’d:\work’] or something like that
# 2. the backup must be stored in a main backup directory
target_dir = ‘/mnt/e/backup/’ # remember to change this to what you will be using
# 3. the files are backed up into a zip file.
# 4. the current day is the name of the subdirectory in the main directory
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 + now + ‘.zip’
# 5. we use the zip command (in unix/linux) to put the files in a zip archive
zip_command = “zip -qr ‘%s’ %s” % (target, ‘ ‘.join(source))
# run the backup
if os.system(zip_command) == 0:
print ‘successful backup to’, target
else:
print ‘backup failed’

输出:

$ python backup_ver2.py
successfully created directory /mnt/e/backup/20041208
successful backup to /mnt/e/backup/20041208/080020.zip
$ python backup_ver2.py
successful backup to /mnt/e/backup/20041208/080428.zip

备份脚本3:

#!/usr/bin/python
# filename: backup_ver4.py
import os
import time
# 1. the files and directories to be backed up are specified in a list.
source = [‘/home/swaroop/byte’, ‘/home/swaroop/bin’]
# if you are using windows, use source = [r’c:\documents’, r’d:\work’] or something like that
# 2. the backup must be stored in a main backup directory
target_dir = ‘/mnt/e/backup/’ # remember to change this to what you will be using
# 3. the files are backed up into a zip file.
# 4. the current day is the name of the subdirectory in the main directory
today = target_dir + time.strftime(‘%y%m%d’)
# the current time is the name of the zip archive
now = time.strftime(‘%h%m%s’)
# take a comment from the user to create the name of the zip file
comment = raw_input(‘enter a comment –> ‘)
if len(comment) == 0: # check if a comment was entered
target = today + os.sep + now + ‘.zip’
else:
target = today + os.sep + now + ‘_’ + \
comment.replace(‘ ‘, ‘_’) + ‘.zip’
# notice the backslash!
# 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
# 5. we use the zip command (in unix/linux) to put the files in a zip archive
zip_command = “zip -qr ‘%s’ %s” % (target, ‘ ‘.join(source))
# run the backup
if os.system(zip_command) == 0:
print ‘successful backup to’, target
else:
print ‘backup failed’

输出:

$ python backup_ver4.py
enter a comment –> added new examples
successful backup to /mnt/e/backup/20041208/082156_added_new_examples.zip
$ python backup_ver4.py
enter a comment –>
successful backup to /mnt/e/backup/20041208/082316.zip

希望本文所述对大家的python程序设计有所帮助。

Posted in 未分类

发表评论