-
Notifications
You must be signed in to change notification settings - Fork 0
/
lbx_lrs_cmd.py
192 lines (173 loc) · 6.17 KB
/
lbx_lrs_cmd.py
1
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# -*- coding: utf-8; -*-
#
# (c) 2007-2010 Mandriva, http://www.mandriva.com/
#
# $Id: lbx_lrs_cmd.py 52 2010-02-11 09:08:24Z nicolas $
#
# This file is part of BeaM
#
# BeaM is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# BeaM is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Pulse 2. If not, see <http://www.gnu.org/licenses/>.
#
"""Class to handle LRS backups
Classes:
LrsThread: to handle LRS binary
"""
__author__ = "Nicolas Rueff <[email protected]>"
__version__ = "$Rev: 52 $"
__date__ = "$Date: 2007-05-31 16:57:13 +0200 (jeu, 31 mai 2007) $"
__licence__ = "GPL"
__copyright__ = "© 2007 Nicolas Rueff - Linbox F&AS"
import os
import gtk
import logging
from beam_consts import *
def halt_backup(pid):
""" Halt a backup using it's pid (using SIGKILL)
"""
if pid != None and pid != 0:
logging.info("LRS Process %d killed" % pid)
os.killpg(os.getpgid(pid), 9)
return False
def run_backup(savepath, binpath, exclude, name, desc, mainwindow):
""" run a backup using LRS binaries
Return the LRS pid
"""
# FIXME: autosave is hardcoded
autosavepath = os.path.join(binpath, BEAM_LRS_AUTOSAVE_NAME)
try: # attempt to create our save dir
os.mkdir(savepath)
logging.info("Creating savepath: |%s|" % savepath)
except OSError, (errno, strerror): # hum, something happen
if errno == 17: # dir exists: don't do anything ? (FIXME: should empty)
pass
else:
# FIXME: should exit ?
logging.error(
"Creation of savepath |%s| failed: %s" %
(savepath, strerror)
)
infopath = os.path.join(savepath, BEAM_LRS_MISC_FOLDER)
realsavepath = os.path.join(savepath, BEAM_LRS_SAVE_FOLDER)
realrestorepath = os.path.join(savepath, BEAM_LRS_RESTORE_FOLDER)
for i in (realsavepath, realrestorepath, infopath):
try: # attempt to create our real save dir
os.mkdir(i)
logging.info("Creating savepath: |%s|" % i)
except OSError, (errno, strerror): # hum, something happen
if errno == 17: # dir exists: clean it
# clean dir (should only contain files)
logging.info("Cleaning savepath |%s|" % i)
for file_in_i in os.listdir(i):
os.unlink(os.path.join(i, file_in_i))
else:
# FIXME: should exit ?
logging.error(
"Creation of |%s| failed: %s" %
(i, strerror)
)
file_exclude = open(os.path.join(infopath, BEAM_LRS_EXCLUDE_NAME), 'w')
logging.info("Creating exclude: |%s|" % infopath)
try:
logging.debug("Exclude contains: %s" % exclude)
file_exclude.write(exclude)
file_exclude.close()
except IOError, (errno, strerror):
logging.error("Creation of exclude failed: %s" %
(savepath, strerror)
)
file_name = open(os.path.join(savepath, BEAM_LRS_NAME_NAME), 'w')
try:
logging.debug("Name contains: %s" % name)
file_name.write(name)
file_name.close()
except IOError, (errno, strerror):
logging.error("Creation of name failed: %s" %
(savepath, strerror)
)
file_desc = open(os.path.join(savepath, BEAM_LRS_DESC_NAME), 'w')
try:
logging.debug("Name contains: %s" % desc)
file_desc.write(desc)
file_desc.close()
except IOError, (errno, strerror):
logging.error("Creation of name failed: %s" %
(savepath, strerror)
)
# run the LRS binaries in a separated fork
try:
pid = os.fork()
if pid > 0: # exit first parent
logging.debug("Fork succeded, LRS PID is: %d" % pid)
return pid
except OSError, (errno, strerror):
logging.info("Fork failed: %s" % strerror)
return False
# decouple from parent environment
mainwindow.socket.close()
mainwindow.destroy()
gtk.main_quit()
os.chdir("/")
os.setsid()
os.umask(0)
args = [autosavepath,
'--nolrs',
'--info', infopath,
'--save', realsavepath,
'--bin', binpath
]
logging.debug("Exec LRS: %s" % args)
os.execvpe(autosavepath, args, os.environ)
def run_restore(savepath, binpath, mainwindow):
""" run a restore using LRS binaries
Return the LRS pid
"""
autorestorepath = os.path.join(binpath, BEAM_LRS_AUTORESTORE_NAME)
realsavepath = os.path.join(savepath, BEAM_LRS_SAVE_FOLDER)
realrestorepath = os.path.join(savepath, BEAM_LRS_RESTORE_FOLDER)
try: # attempt to create our real save dir
os.mkdir(realrestorepath)
logging.info("Creating savepath: |%s|" % realrestorepath)
except OSError, (errno, strerror): # hum, something happen
if errno == 17: # dir exists: do not touch it
pass
else:
# FIXME: should exit ?
logging.error(
"Creation of |%s| failed: %s" %
(realrestorepath, strerror)
)
# run the LRS binaries in a separated fork
try:
pid = os.fork()
if pid > 0: # exit first parent
logging.debug("Fork succeded, LRS PID is: %d" % pid)
return pid
except OSError, (errno, strerror):
logging.info("Fork failed: %s" % strerror)
return False
# decouple from parent environment
mainwindow.socket.close()
mainwindow.destroy()
gtk.main_quit()
os.chdir("/")
os.setsid()
os.umask(0)
args = [autorestorepath,
'--nolrs',
'--info', realrestorepath,
'--save', realsavepath,
'--bin', binpath
]
logging.debug("Exec LRS: %s" % args)
os.execvpe(autorestorepath, args, os.environ)