天天看點

【python】簡單的備份腳本2

根據日期建立檔案夾并存放備份的檔案。

# 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

    print 'backup failed'

"backup_ver4.py" 42l, 1449c written                                                                                                                  

<b>[yang@rac1 ~]$ python backup_ver2.py</b>

enter a comment --&gt; yangql

successfully created directory /tmp/20110723222525

successful backup to /tmp/20110723222525/20110723222525_yangql.zip