1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 __doc__='''ZenBackupBase
17
18 Common code for zenbackup.py and zenrestore.py
19 '''
20 import tempfile
21 from subprocess import Popen, PIPE
22
23 from CmdBase import CmdBase
24
25
26 BACKUP_DIR = 'zenbackup'
27 CONFIG_FILE = 'backup.settings'
28 CONFIG_SECTION = 'zenbackup'
29
30 CONFIG_FIELDS = ( ('dbname', 'events', 'database'),
31 ('dbuser', 'root', 'username'),
32 ('dbpass', '', 'password'),
33 ('dbhost', 'localhost', 'host'),
34 ('dbport', '3306', 'port'),
35 )
36
37
39 doesLogging = False
40
41
44
46 '''
47 If --verbose then send msg to stdout
48 '''
49 if self.options.verbose:
50 print(msg)
51
52
54 """
55 Command-line options setup
56 """
57 CmdBase.buildOptions(self)
58 self.parser.add_option('-v', '--verbose',
59 dest="verbose",
60 default=False,
61 action='store_true',
62 help='Send progress messages to stdout.')
63 self.parser.add_option('--temp-dir',
64 dest="tempDir",
65 default=None,
66 help='Directory to use for temporary storage.')
67
68
70 '''
71 Return string to be used as the -p (including the "-p")
72 to MySQL commands.
73
74 @return: password and flag
75 @rtype: string
76 '''
77 if self.options.dbpass == None:
78 return ''
79 return '--password="%s"' % self.options.dbpass
80
81
83 '''
84 Return directory to be used for temporary storage
85 during backup or restore.
86
87 @return: directory name
88 @rtype: string
89 '''
90 if self.options.tempDir:
91 dir = tempfile.mkdtemp('', '', self.options.tempDir)
92 else:
93 dir = tempfile.mkdtemp()
94 return dir
95
96
97 - def runCommand(self, cmd=[], obfuscated_cmd=None):
98 """
99 Execute a command and return the results, displaying pre and
100 post messages.
101
102 @parameter cmd: command to run
103 @type cmd: list
104 @return: results of the command (output, warnings, returncode)
105 """
106 if obfuscated_cmd:
107 self.log.debug(' '.join(obfuscated_cmd))
108 else:
109 self.log.debug(' '.join(cmd))
110
111 proc = Popen(cmd, stdout=PIPE, stderr=PIPE)
112 output, warnings = proc.communicate()
113 if proc.returncode:
114 self.log.warn(warnings)
115 self.log.debug(output or 'No output from command')
116 return (output, warnings, proc.returncode)
117