Chinaunix首页 | 论坛 | 博客
  • 博客访问: 624561
  • 博文数量: 110
  • 博客积分: 3808
  • 博客等级: 中校
  • 技术积分: 1930
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-15 14:32
个人简介

声的伟大!

文章分类

全部博文(110)

文章存档

2014年(1)

2013年(2)

2012年(12)

2011年(81)

2010年(14)

分类: Python/Ruby

2013-03-28 09:53:36

#!/usr/bin/python
# Filename: backup.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' 
阅读(1702) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~