author | Tero Marttila <terom@paivola.fi> |
Fri, 02 Mar 2012 15:52:25 +0200 | |
changeset 30 | 29b60df79122 |
parent 28 | 82bcde9e21c4 |
child 32 | 9d623185d3d7 |
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") |
|
46 |
||
47 |
parser.add_option_group(general) |
|
48 |
||
49 |
# |
|
50 |
parser.add_option('-c', '--command', metavar='CMD', default=os.environ.get('SSH_ORIGINAL_COMMAND'), |
|
3 | 51 |
help="rsync command to execute") |
52 |
||
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
|
53 |
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
|
54 |
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
|
55 |
|
3 | 56 |
parser.add_option('-R', '--readonly', action='store_true', default=False, |
8 | 57 |
help="restrict to read/source mode") |
3 | 58 |
|
8 | 59 |
parser.add_option('-P', '--restrict-path', metavar='PATH', default=False, |
60 |
help="restrict to given path prefix") |
|
3 | 61 |
|
62 |
# defaults |
|
63 |
parser.set_defaults( |
|
64 |
loglevel = logging.WARNING, |
|
65 |
) |
|
66 |
||
67 |
# parse |
|
68 |
options, args = parser.parse_args(argv[1:]) |
|
69 |
||
70 |
# configure |
|
71 |
logging.basicConfig( |
|
11 | 72 |
format = '%(levelname)6s %(name)s : %(funcName)s : %(message)s', |
3 | 73 |
level = options.loglevel, |
74 |
) |
|
75 |
||
76 |
return options, args |
|
77 |
||
78 |
def rsync_wrapper (command, restrict='lvm:') : |
|
79 |
""" |
|
80 |
Wrap given rsync command. |
|
8 | 81 |
|
82 |
Parses the command, the source path, and then executes rsync within the source path (which may be a special |
|
83 |
pseudo-path with additional handling). |
|
3 | 84 |
""" |
85 |
||
86 |
try : |
|
8 | 87 |
# 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
|
88 |
rsync_cmd, rsync_options, source_path, dest_path = rsync.parse_command(command, |
3 | 89 |
restrict_readonly = options.readonly, |
90 |
) |
|
91 |
||
92 |
except RSyncCommandFormatError, e: |
|
93 |
log.error("invalid rsync command: %r: %s", command, e) |
|
94 |
return 2 |
|
95 |
||
96 |
# XXX: the real path is always given second.. |
|
97 |
path = dest_path |
|
98 |
||
99 |
try : |
|
8 | 100 |
# 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
|
101 |
source = rsync.parse_source(path, |
3 | 102 |
restrict_path = options.restrict_path, |
103 |
) |
|
104 |
||
105 |
except RSyncCommandFormatError, e: |
|
106 |
log.error("invalid rsync source: %r: %s", path, e) |
|
107 |
return 2 |
|
108 |
||
109 |
try : |
|
8 | 110 |
# run rsync within the source (may perform additional stuff like snapshotting...) |
3 | 111 |
source.execute(rsync_options) |
112 |
||
113 |
except InvokeError, e: |
|
114 |
log.error("%s failed: %d", e.cmd, e.exit) |
|
115 |
return e.exit |
|
116 |
||
117 |
# ok |
|
118 |
return 0 |
|
119 |
||
120 |
def main (argv) : |
|
121 |
global options |
|
122 |
||
123 |
# global options + args |
|
124 |
options, args = parse_options(argv) |
|
125 |
||
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
|
126 |
# 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
|
127 |
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
|
128 |
# 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
|
129 |
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
|
130 |
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
|
131 |
|
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 |
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
|
133 |
|
3 | 134 |
# 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
|
135 |
elif args : |
3 | 136 |
log.error("No arguments are handled") |
137 |
return 2 |
|
138 |
||
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
|
139 |
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
|
140 |
# 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
|
141 |
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
|
142 |
|
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 |
else : |
3 | 144 |
log.error("SSH_ORIGINAL_COMMAND not given") |
145 |
return 2 |
|
146 |
||
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
|
147 |
|
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 |
# run |
3 | 149 |
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
|
150 |
return rsync_wrapper(command_parts) |
3 | 151 |
|
152 |
except Exception, e: |
|
153 |
log.error("Internal error:", exc_info=e) |
|
154 |
return 3 |
|
155 |
||
156 |
# ok |
|
157 |
return 0 |
|
0
d5fd342015d3
initial rsync-lvm-server script
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
158 |
|
d5fd342015d3
initial rsync-lvm-server script
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
159 |
if __name__ == '__main__' : |
d5fd342015d3
initial rsync-lvm-server script
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
160 |
import sys |
d5fd342015d3
initial rsync-lvm-server script
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
161 |
|
d5fd342015d3
initial rsync-lvm-server script
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
162 |
sys.exit(main(sys.argv)) |
d5fd342015d3
initial rsync-lvm-server script
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
163 |