author | Tero Marttila <terom@paivola.fi> |
Sun, 22 Apr 2012 12:31:26 +0300 | |
changeset 42 | 43e27a3e9efe |
parent 41 | 921b45c4678b |
permissions | -rwxr-xr-x |
0
d5fd342015d3
initial rsync-lvm-server script
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
1 |
#!/usr/bin/python |
d5fd342015d3
initial rsync-lvm-server script
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
2 |
|
8 | 3 |
""" |
4 |
SSH authorized_keys command="..." wrapper for rsync. |
|
28
82bcde9e21c4
rsync: pass command as list, moving the shlex.split to main; support `rsync -e "rsync-wrapper -C --"` style execution with the rsync command given as arguments
Tero Marttila <terom@paivola.fi>
parents:
12
diff
changeset
|
5 |
|
82bcde9e21c4
rsync: pass command as list, moving the shlex.split to main; support `rsync -e "rsync-wrapper -C --"` style execution with the rsync command given as arguments
Tero Marttila <terom@paivola.fi>
parents:
12
diff
changeset
|
6 |
Testing goes something like: |
42
43e27a3e9efe
pvlbackup-rsync-wrapper: add --snapshot-size / --snapshot-wait opts
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
7 |
sudo PYTHONPATH=. ./bin/pvlbackup-rsync-wrapper --command 'rsync --server --sender -ax . lvm:asdf:test' -vD |
43e27a3e9efe
pvlbackup-rsync-wrapper: add --snapshot-size / --snapshot-wait opts
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
8 |
|
43e27a3e9efe
pvlbackup-rsync-wrapper: add --snapshot-size / --snapshot-wait opts
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
9 |
sudo sh -c "PYTHONPATH=. rsync -e './bin/pvlbackup-rsync-wrapper --debug -C --' -ax testing:lvm:asdf:test test/tmp" |
8 | 10 |
""" |
11 |
||
30
29b60df79122
version: 0.2.3; move version to pvl.backup.__version__; add --version opt
Tero Marttila <terom@paivola.fi>
parents:
28
diff
changeset
|
12 |
from pvl.backup import __version__ |
5
23371d26fdd0
split up into pvl.backup package
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
13 |
from pvl.backup.rsync import RSyncCommandFormatError |
23371d26fdd0
split up into pvl.backup package
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
14 |
from pvl.backup.invoke import InvokeError |
42
43e27a3e9efe
pvlbackup-rsync-wrapper: add --snapshot-size / --snapshot-wait opts
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
15 |
from pvl.backup import rsync, lvm |
3 | 16 |
|
5
23371d26fdd0
split up into pvl.backup package
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
17 |
import optparse |
28
82bcde9e21c4
rsync: pass command as list, moving the shlex.split to main; support `rsync -e "rsync-wrapper -C --"` style execution with the rsync command given as arguments
Tero Marttila <terom@paivola.fi>
parents:
12
diff
changeset
|
18 |
import shlex |
5
23371d26fdd0
split up into pvl.backup package
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
19 |
import os |
0
d5fd342015d3
initial rsync-lvm-server script
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
20 |
import logging |
d5fd342015d3
initial rsync-lvm-server script
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
21 |
|
d5fd342015d3
initial rsync-lvm-server script
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
22 |
log = logging.getLogger() |
d5fd342015d3
initial rsync-lvm-server script
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
23 |
|
3 | 24 |
# command-line options |
25 |
options = None |
|
26 |
||
27 |
def parse_options (argv) : |
|
28 |
""" |
|
29 |
Parse command-line arguments. |
|
30 |
""" |
|
31 |
||
28
82bcde9e21c4
rsync: pass command as list, moving the shlex.split to main; support `rsync -e "rsync-wrapper -C --"` style execution with the rsync command given as arguments
Tero Marttila <terom@paivola.fi>
parents:
12
diff
changeset
|
32 |
# import sys; sys.stderr.write("%s\n" % (argv, )) |
82bcde9e21c4
rsync: pass command as list, moving the shlex.split to main; support `rsync -e "rsync-wrapper -C --"` style execution with the rsync command given as arguments
Tero Marttila <terom@paivola.fi>
parents:
12
diff
changeset
|
33 |
|
8 | 34 |
parser = optparse.OptionParser( |
35 |
prog = argv[0], |
|
3 | 36 |
|
8 | 37 |
# module docstring |
38 |
description = __doc__, |
|
30
29b60df79122
version: 0.2.3; move version to pvl.backup.__version__; add --version opt
Tero Marttila <terom@paivola.fi>
parents:
28
diff
changeset
|
39 |
version = __version__, |
8 | 40 |
) |
3 | 41 |
|
42 |
# logging |
|
8 | 43 |
general = optparse.OptionGroup(parser, "General Options") |
3 | 44 |
|
8 | 45 |
general.add_option('-q', '--quiet', dest='loglevel', action='store_const', const=logging.WARNING, help="Less output") |
46 |
general.add_option('-v', '--verbose', dest='loglevel', action='store_const', const=logging.INFO, help="More output") |
|
47 |
general.add_option('-D', '--debug', dest='loglevel', action='store_const', const=logging.DEBUG, help="Even more output") |
|
32
9d623185d3d7
rsync-wrapper: targeted --debug-for specific loggers
Tero Marttila <terom@paivola.fi>
parents:
30
diff
changeset
|
48 |
general.add_option('--debug-for', action='append', metavar='MODULE', help="Enable logging for the given logger/module name") |
8 | 49 |
|
50 |
parser.add_option_group(general) |
|
51 |
||
52 |
# |
|
53 |
parser.add_option('-c', '--command', metavar='CMD', default=os.environ.get('SSH_ORIGINAL_COMMAND'), |
|
3 | 54 |
help="rsync command to execute") |
55 |
||
28
82bcde9e21c4
rsync: pass command as list, moving the shlex.split to main; support `rsync -e "rsync-wrapper -C --"` style execution with the rsync command given as arguments
Tero Marttila <terom@paivola.fi>
parents:
12
diff
changeset
|
56 |
parser.add_option('-C', '--given-command', action='store_true', default=False, |
82bcde9e21c4
rsync: pass command as list, moving the shlex.split to main; support `rsync -e "rsync-wrapper -C --"` style execution with the rsync command given as arguments
Tero Marttila <terom@paivola.fi>
parents:
12
diff
changeset
|
57 |
help="use given command in `rsync -e %prog` format") |
82bcde9e21c4
rsync: pass command as list, moving the shlex.split to main; support `rsync -e "rsync-wrapper -C --"` style execution with the rsync command given as arguments
Tero Marttila <terom@paivola.fi>
parents:
12
diff
changeset
|
58 |
|
3 | 59 |
parser.add_option('-R', '--readonly', action='store_true', default=False, |
8 | 60 |
help="restrict to read/source mode") |
3 | 61 |
|
8 | 62 |
parser.add_option('-P', '--restrict-path', metavar='PATH', default=False, |
63 |
help="restrict to given path prefix") |
|
3 | 64 |
|
42
43e27a3e9efe
pvlbackup-rsync-wrapper: add --snapshot-size / --snapshot-wait opts
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
65 |
# lvm options |
43e27a3e9efe
pvlbackup-rsync-wrapper: add --snapshot-size / --snapshot-wait opts
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
66 |
parser.add_option('-L', '--snapshot-size', metavar='SIZE', default=lvm.LVM_SNAPSHOT_SIZE, |
43e27a3e9efe
pvlbackup-rsync-wrapper: add --snapshot-size / --snapshot-wait opts
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
67 |
help="create snapshot with given LV size (used to store writes during backup)") |
43e27a3e9efe
pvlbackup-rsync-wrapper: add --snapshot-size / --snapshot-wait opts
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
68 |
|
43e27a3e9efe
pvlbackup-rsync-wrapper: add --snapshot-size / --snapshot-wait opts
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
69 |
parser.add_option('--snapshot-wait', metavar='SECONDS', default=lvm.LVM_SNAPSHOT_WAIT, type='float', |
43e27a3e9efe
pvlbackup-rsync-wrapper: add --snapshot-size / --snapshot-wait opts
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
70 |
help="wait for snapshot to settle after unmounting") |
43e27a3e9efe
pvlbackup-rsync-wrapper: add --snapshot-size / --snapshot-wait opts
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
71 |
|
3 | 72 |
# defaults |
73 |
parser.set_defaults( |
|
32
9d623185d3d7
rsync-wrapper: targeted --debug-for specific loggers
Tero Marttila <terom@paivola.fi>
parents:
30
diff
changeset
|
74 |
debug_for = [], |
34
8c23dc925ed7
rsync-wrapper: default to logging.INFO; cleanup pvl.backup.rsync info output
Tero Marttila <terom@paivola.fi>
parents:
32
diff
changeset
|
75 |
loglevel = logging.INFO, |
3 | 76 |
) |
77 |
||
78 |
# parse |
|
79 |
options, args = parser.parse_args(argv[1:]) |
|
80 |
||
81 |
# configure |
|
82 |
logging.basicConfig( |
|
11 | 83 |
format = '%(levelname)6s %(name)s : %(funcName)s : %(message)s', |
3 | 84 |
level = options.loglevel, |
85 |
) |
|
86 |
||
32
9d623185d3d7
rsync-wrapper: targeted --debug-for specific loggers
Tero Marttila <terom@paivola.fi>
parents:
30
diff
changeset
|
87 |
# enable debugging for specific targets |
9d623185d3d7
rsync-wrapper: targeted --debug-for specific loggers
Tero Marttila <terom@paivola.fi>
parents:
30
diff
changeset
|
88 |
for target in options.debug_for : |
9d623185d3d7
rsync-wrapper: targeted --debug-for specific loggers
Tero Marttila <terom@paivola.fi>
parents:
30
diff
changeset
|
89 |
logging.getLogger(target).setLevel(logging.DEBUG) |
9d623185d3d7
rsync-wrapper: targeted --debug-for specific loggers
Tero Marttila <terom@paivola.fi>
parents:
30
diff
changeset
|
90 |
|
3 | 91 |
return options, args |
92 |
||
93 |
def rsync_wrapper (command, restrict='lvm:') : |
|
94 |
""" |
|
95 |
Wrap given rsync command. |
|
8 | 96 |
|
97 |
Parses the command, the source path, and then executes rsync within the source path (which may be a special |
|
98 |
pseudo-path with additional handling). |
|
3 | 99 |
""" |
100 |
||
101 |
try : |
|
8 | 102 |
# parse the rsync command sent by the client |
5
23371d26fdd0
split up into pvl.backup package
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
103 |
rsync_cmd, rsync_options, source_path, dest_path = rsync.parse_command(command, |
3 | 104 |
restrict_readonly = options.readonly, |
105 |
) |
|
106 |
||
107 |
except RSyncCommandFormatError, e: |
|
108 |
log.error("invalid rsync command: %r: %s", command, e) |
|
109 |
return 2 |
|
110 |
||
111 |
# XXX: the real path is always given second.. |
|
112 |
path = dest_path |
|
113 |
||
114 |
try : |
|
8 | 115 |
# parse the source path as given by the client, may be a real path or pseudo-path |
5
23371d26fdd0
split up into pvl.backup package
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
116 |
source = rsync.parse_source(path, |
3 | 117 |
restrict_path = options.restrict_path, |
42
43e27a3e9efe
pvlbackup-rsync-wrapper: add --snapshot-size / --snapshot-wait opts
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
118 |
lvm_opts = dict(size = options.snapshot_size, wait = options.snapshot_wait), |
3 | 119 |
) |
120 |
||
121 |
except RSyncCommandFormatError, e: |
|
122 |
log.error("invalid rsync source: %r: %s", path, e) |
|
123 |
return 2 |
|
124 |
||
125 |
try : |
|
8 | 126 |
# run rsync within the source (may perform additional stuff like snapshotting...) |
3 | 127 |
source.execute(rsync_options) |
128 |
||
129 |
except InvokeError, e: |
|
130 |
log.error("%s failed: %d", e.cmd, e.exit) |
|
131 |
return e.exit |
|
132 |
||
133 |
# ok |
|
134 |
return 0 |
|
135 |
||
136 |
def main (argv) : |
|
137 |
global options |
|
138 |
||
139 |
# global options + args |
|
140 |
options, args = parse_options(argv) |
|
141 |
||
28
82bcde9e21c4
rsync: pass command as list, moving the shlex.split to main; support `rsync -e "rsync-wrapper -C --"` style execution with the rsync command given as arguments
Tero Marttila <terom@paivola.fi>
parents:
12
diff
changeset
|
142 |
# command required |
82bcde9e21c4
rsync: pass command as list, moving the shlex.split to main; support `rsync -e "rsync-wrapper -C --"` style execution with the rsync command given as arguments
Tero Marttila <terom@paivola.fi>
parents:
12
diff
changeset
|
143 |
if options.given_command : |
82bcde9e21c4
rsync: pass command as list, moving the shlex.split to main; support `rsync -e "rsync-wrapper -C --"` style execution with the rsync command given as arguments
Tero Marttila <terom@paivola.fi>
parents:
12
diff
changeset
|
144 |
# from args (as given by `rsync -e pvlbackup-rsync-wrapper`) -> 'pvlbackup-rsync-wrapper <host> (<command> ...)' |
82bcde9e21c4
rsync: pass command as list, moving the shlex.split to main; support `rsync -e "rsync-wrapper -C --"` style execution with the rsync command given as arguments
Tero Marttila <terom@paivola.fi>
parents:
12
diff
changeset
|
145 |
host = args.pop(0) |
82bcde9e21c4
rsync: pass command as list, moving the shlex.split to main; support `rsync -e "rsync-wrapper -C --"` style execution with the rsync command given as arguments
Tero Marttila <terom@paivola.fi>
parents:
12
diff
changeset
|
146 |
command_parts = args |
82bcde9e21c4
rsync: pass command as list, moving the shlex.split to main; support `rsync -e "rsync-wrapper -C --"` style execution with the rsync command given as arguments
Tero Marttila <terom@paivola.fi>
parents:
12
diff
changeset
|
147 |
|
42
43e27a3e9efe
pvlbackup-rsync-wrapper: add --snapshot-size / --snapshot-wait opts
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
148 |
log.debug("host=%r, using command from args: %r", host, command_parts) |
28
82bcde9e21c4
rsync: pass command as list, moving the shlex.split to main; support `rsync -e "rsync-wrapper -C --"` style execution with the rsync command given as arguments
Tero Marttila <terom@paivola.fi>
parents:
12
diff
changeset
|
149 |
|
3 | 150 |
# args |
28
82bcde9e21c4
rsync: pass command as list, moving the shlex.split to main; support `rsync -e "rsync-wrapper -C --"` style execution with the rsync command given as arguments
Tero Marttila <terom@paivola.fi>
parents:
12
diff
changeset
|
151 |
elif args : |
3 | 152 |
log.error("No arguments are handled") |
153 |
return 2 |
|
154 |
||
28
82bcde9e21c4
rsync: pass command as list, moving the shlex.split to main; support `rsync -e "rsync-wrapper -C --"` style execution with the rsync command given as arguments
Tero Marttila <terom@paivola.fi>
parents:
12
diff
changeset
|
155 |
elif options.command: |
82bcde9e21c4
rsync: pass command as list, moving the shlex.split to main; support `rsync -e "rsync-wrapper -C --"` style execution with the rsync command given as arguments
Tero Marttila <terom@paivola.fi>
parents:
12
diff
changeset
|
156 |
# as given |
82bcde9e21c4
rsync: pass command as list, moving the shlex.split to main; support `rsync -e "rsync-wrapper -C --"` style execution with the rsync command given as arguments
Tero Marttila <terom@paivola.fi>
parents:
12
diff
changeset
|
157 |
command_parts = shlex.split(options.command) |
82bcde9e21c4
rsync: pass command as list, moving the shlex.split to main; support `rsync -e "rsync-wrapper -C --"` style execution with the rsync command given as arguments
Tero Marttila <terom@paivola.fi>
parents:
12
diff
changeset
|
158 |
|
82bcde9e21c4
rsync: pass command as list, moving the shlex.split to main; support `rsync -e "rsync-wrapper -C --"` style execution with the rsync command given as arguments
Tero Marttila <terom@paivola.fi>
parents:
12
diff
changeset
|
159 |
else : |
3 | 160 |
log.error("SSH_ORIGINAL_COMMAND not given") |
161 |
return 2 |
|
162 |
||
28
82bcde9e21c4
rsync: pass command as list, moving the shlex.split to main; support `rsync -e "rsync-wrapper -C --"` style execution with the rsync command given as arguments
Tero Marttila <terom@paivola.fi>
parents:
12
diff
changeset
|
163 |
|
82bcde9e21c4
rsync: pass command as list, moving the shlex.split to main; support `rsync -e "rsync-wrapper -C --"` style execution with the rsync command given as arguments
Tero Marttila <terom@paivola.fi>
parents:
12
diff
changeset
|
164 |
# run |
3 | 165 |
try : |
28
82bcde9e21c4
rsync: pass command as list, moving the shlex.split to main; support `rsync -e "rsync-wrapper -C --"` style execution with the rsync command given as arguments
Tero Marttila <terom@paivola.fi>
parents:
12
diff
changeset
|
166 |
return rsync_wrapper(command_parts) |
3 | 167 |
|
168 |
except Exception, e: |
|
169 |
log.error("Internal error:", exc_info=e) |
|
170 |
return 3 |
|
171 |
||
172 |
# ok |
|
173 |
return 0 |
|
0
d5fd342015d3
initial rsync-lvm-server script
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
174 |
|
d5fd342015d3
initial rsync-lvm-server script
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
175 |
if __name__ == '__main__' : |
d5fd342015d3
initial rsync-lvm-server script
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
176 |
import sys |
d5fd342015d3
initial rsync-lvm-server script
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
177 |
|
d5fd342015d3
initial rsync-lvm-server script
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
178 |
sys.exit(main(sys.argv)) |
d5fd342015d3
initial rsync-lvm-server script
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
179 |