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