Line | |
---|
1 | |
---|
2 | import tempfile |
---|
3 | import shutil |
---|
4 | import os |
---|
5 | import subprocess |
---|
6 | import time |
---|
7 | import tarfile |
---|
8 | |
---|
9 | class CupsManager: |
---|
10 | |
---|
11 | |
---|
12 | def __init__(self): |
---|
13 | |
---|
14 | self.backup_files=["/etc/cups/cupsd.conf","/etc/cups/printers.conf"] |
---|
15 | self.backup_dirs=["/etc/cups/ppd/"] |
---|
16 | |
---|
17 | |
---|
18 | #def init |
---|
19 | |
---|
20 | def get_time(self): |
---|
21 | |
---|
22 | return get_backup_name("CupsManager") |
---|
23 | |
---|
24 | #def get_time |
---|
25 | |
---|
26 | |
---|
27 | def backup(self,dir="/backup/"): |
---|
28 | |
---|
29 | try: |
---|
30 | |
---|
31 | file_path=dir+"/"+self.get_time() |
---|
32 | tar=tarfile.open(file_path,"w:gz") |
---|
33 | |
---|
34 | for f in self.backup_files: |
---|
35 | if os.path.exists(f): |
---|
36 | tar.add(f) |
---|
37 | |
---|
38 | for d in self.backup_dirs: |
---|
39 | if os.path.exists(d): |
---|
40 | tar.add(d) |
---|
41 | |
---|
42 | tar.close() |
---|
43 | |
---|
44 | return [True,file_path] |
---|
45 | |
---|
46 | except Exception as e: |
---|
47 | return [False,str(e)] |
---|
48 | |
---|
49 | #def test |
---|
50 | |
---|
51 | def restore(self,file_path=None): |
---|
52 | |
---|
53 | |
---|
54 | if file_path==None: |
---|
55 | for f in sorted(os.listdir("/backup"),reverse=True): |
---|
56 | if "CupsManager" in f: |
---|
57 | file_path="/backup/"+f |
---|
58 | break |
---|
59 | |
---|
60 | if file_path==None: |
---|
61 | return [False,"Backup file not found"] |
---|
62 | |
---|
63 | try: |
---|
64 | |
---|
65 | if os.path.exists(file_path): |
---|
66 | |
---|
67 | tmp_dir=tempfile.mkdtemp() |
---|
68 | tar=tarfile.open(file_path) |
---|
69 | tar.extractall(tmp_dir) |
---|
70 | tar.close() |
---|
71 | |
---|
72 | for f in self.backup_files: |
---|
73 | tmp_path=tmp_dir+f |
---|
74 | if os.path.exists(tmp_path): |
---|
75 | shutil.copy(tmp_path,f) |
---|
76 | |
---|
77 | for d in self.backup_dirs: |
---|
78 | tmp_path=tmp_dir+d |
---|
79 | if os.path.exists(tmp_path): |
---|
80 | cmd="cp -r " + tmp_path +"/* " + d |
---|
81 | os.system(cmd) |
---|
82 | |
---|
83 | os.system("service cups restart") |
---|
84 | |
---|
85 | return [True,""] |
---|
86 | |
---|
87 | except Exception as e: |
---|
88 | |
---|
89 | return [False,str(e)] |
---|
90 | |
---|
91 | #def test |
---|
92 | |
---|
93 | |
---|
94 | |
---|
95 | |
---|
96 | #class CupsManager |
---|
97 | |
---|
98 | |
---|
Note: See
TracBrowser
for help on using the repository browser.