【python】简单的备份脚本2

简介: 根据日期建立文件夹并存放备份的文件。# 4. The current day is the name of the subdirectory in the main directorytoday = target_dir + time.
根据日期建立文件夹并存放备份的文件。
# 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
目录
相关文章
|
2月前
|
存储 数据库 Python
Loguru:Python中强大的日志库
Loguru:Python中强大的日志库
122 2
|
6月前
|
Python
38 python - 文件备份
38 python - 文件备份
16 0
|
索引 Python
用python写了个算命脚本
用python写了个算命脚本
|
开发框架 并行计算 IDE
Python 脚本一个要注意的点
Python 脚本一个要注意的点
Python 脚本一个要注意的点