[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)