ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 【python】简单的备份脚本2

【python】简单的备份脚本2

原创 Linux操作系统 作者:杨奇龙 时间:2011-07-24 01:06:57 0 删除 编辑
根据日期建立文件夹并存放备份的文件。
# 4. The current day is the name of the subdirectory in the main directory
today = target_dir + time.strftime('%Y%m%d%H%M%S')
#print today
# The current time is the name of the zip archive
now = time.strftime('%Y%m%d%H%M%S')
#print now
# 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, source)
#print zip_command
# Run the backup
if os.system(zip_command) == 0:
    print 'Successful backup to', target
else:
    print 'Backup FAILED'
"backup_ver4.py" 42L, 1449C written                                                                                                                  
[yang@rac1 ~]$ python backup_ver2.py
Enter a comment --> yangql
Successfully created directory /tmp/20110723222525
Successful backup to /tmp/20110723222525/20110723222525_yangql.zip

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/22664653/viewspace-702947/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
MySQL DBA NoSQL DEVOPS

注册时间:2009-10-07

  • 博文量
    1026
  • 访问量
    7845798