标签:python
[root@saltstack-ui tmp]# cat backup.py #!/usr/bin/env python import os import time import sys def create_backup_dir(target_dir): today_dir = target_dir + time.strftime(‘%Y-%m-%d‘) if not os.path.exists(today_dir): os.mkdir(today_dir) return today_dir def is_exists_dir(source): for dir in source: if not os.path.exists(dir): print "Source %s does not exist" % dir sys.exit(1) def build_backup_command(source, target_dir): is_exists_dir(source) time_dir = time.strftime(‘%H-%M-%S‘) today_dir = create_backup_dir(target_dir) touch = today_dir + os.sep + time_dir + ‘.zip‘ # os.sep 根据操作系统不同,形成不同的文件路径分隔符 zip_command = "zip -qr %s %s" % (touch, ‘ ‘.join(source)) return zip_command def exec_backup_command(zip_command): if os.system(zip_command) == 0: print "Backup successfully" else: print "Backup failed" if __name__ == ‘__main__‘: source = [‘/tmp/a/‘] target_dir = ‘/tmp/b/‘ zip_command = build_backup_command(source, target_dir) exec_backup_command(zip_command)
本文出自 “the-way-to-cloud” 博客,请务必保留此出处http://iceyao.blog.51cto.com/9426658/1701374
标签:python
原文地址:http://iceyao.blog.51cto.com/9426658/1701374