【python】簡單的備份指令碼2

楊奇龍發表於2011-07-24
根據日期建立資料夾並存放備份的檔案。
# 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 --&gt ')
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 --&gt yangql
Successfully created directory /tmp/20110723222525
Successful backup to /tmp/20110723222525/20110723222525_yangql.zip

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22664653/viewspace-702947/,如需轉載,請註明出處,否則將追究法律責任。

相關文章