|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
| [iyunv@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)
|
|
|