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