This adds the option to rename paths when saving them.
A directory /root/chroot/a/etc saved with "bup save -n chroots
--graft /root/chroot/a/etc=/chroots/a" would be saved as
/chroots/a/etc.
Signed-off-by: Zoran Zaric <zz@zoranzaric.de>
"bup save -n webserver --strip-path=/root/chroots" would
be saved as */webserver/etc*
+--graft=*old_path*=*new_path*
+: a graft point *old_path*=*new_path* (can be used more than
+ once).
+
+ A directory */root/chroot/a/etc* saved with
+ "bup save -n chroots --graft /root/chroot/a/etc=/chroots/a"
+ would be saved as */chroots/a/etc*
# EXAMPLE
f,indexfile= the name of the index file (normally BUP_DIR/bupindex)
strip strips the path to every filename given
strip-path= path-prefix to be stripped when saving
+graft= a graft point *old_path*=*new_path* (can be used morethan once)
"""
o = options.Options('bup save', optspec)
(opt, flags, extra) = o.parse(sys.argv[1:])
if opt.strip and opt.strip_path:
o.fatal("--strip is incompatible with --strip-path")
+graft_points = []
+if opt.graft:
+ if opt.strip:
+ o.fatal("--strip is incompatible with --graft")
+
+ if opt.strip_path:
+ o.fatal("--strip-path is incompatible with --graft")
+
+ for (option, parameter) in flags:
+ if option == "--graft":
+ splitted_parameter = parameter.split('=')
+ if len(splitted_parameter) != 2:
+ o.fatal("a graft point must be of the form old_path=new_path")
+ graft_points.append((realpath(splitted_parameter[0]),
+ realpath(splitted_parameter[1])))
+
is_reverse = os.environ.get('BUP_SERVER_REVERSE')
if is_reverse and opt.remote:
o.fatal("don't use -r in reverse mode; it's automatic")
dirp = stripped_base_path.split('/')
elif opt.strip_path:
dirp = strip_path(opt.strip_path, dir).split('/')
+ elif graft_points:
+ grafted = graft_path(graft_points, dir)
+ dirp = grafted.split('/')
else:
dirp = dir.split('/')
while parts > dirp:
"""Helper functions and classes for bup."""
+
import sys, os, pwd, subprocess, errno, socket, select, mmap, stat, re
from bup import _version
def strip_base_path(path, base_paths):
"""Strips the base path from a given path.
+
Determines the base path for the given string and the strips it
using strip_path().
Iterates over all base_paths from long to short, to prevent that
return strip_path(bp, path)
return path
+def graft_path(graft_points, path):
+ normalized_path = realpath(path)
+ for graft_point in graft_points:
+ old_prefix, new_prefix = graft_point
+ if normalized_path.startswith(old_prefix):
+ return re.sub(r'^' + old_prefix, new_prefix, normalized_path)
+ return normalized_path
+
# hashlib is only available in python 2.5 or higher, but the 'sha' module
# produces a DeprecationWarning in python 2.6 or higher. We want to support
path = "/var/backup/daily.0/localhost/etc/"
base_paths = ["/var", "/var/backup", "/var/backup/daily.0/localhost"]
WVPASSEQ(strip_base_path(path, base_paths), '/etc')
+
+@wvtest
+def test_graft_path():
+ middle_matching_old_path = "/user"
+ non_matching_old_path = "/usr"
+ matching_old_path = "/home"
+ matching_full_path = "/home/user"
+ new_path = "/opt"
+
+ all_graft_points = [(middle_matching_old_path, new_path),
+ (non_matching_old_path, new_path),
+ (matching_old_path, new_path)]
+
+ path = "/home/user/"
+
+ WVPASSEQ(graft_path([(middle_matching_old_path, new_path)], path),
+ "/home/user")
+ WVPASSEQ(graft_path([(non_matching_old_path, new_path)], path),
+ "/home/user")
+ WVPASSEQ(graft_path([(matching_old_path, new_path)], path), "/opt/user")
+ WVPASSEQ(graft_path(all_graft_points, path), "/opt/user")
+ WVPASSEQ(graft_path([(matching_full_path, new_path)], path),
+ "/opt")
d/
f"
+WVSTART "graft_points"
+D=graft-points.tmp
+rm -rf $D
+mkdir $D
+export BUP_DIR="$D/.bup"
+WVPASS bup init
+touch $D/a
+WVPASS bup random 128k >$D/b
+mkdir $D/d $D/d/e
+WVPASS bup random 512 >$D/f
+WVPASS bup index -ux $D
+bup save --graft $TOP/$D=/grafted -n graft-point-absolute $D
+WVPASSEQ "$(bup ls graft-point-absolute/latest/grafted/)" "a
+b
+d/
+f"
+bup save --graft $D=grafted -n graft-point-relative $D
+WVPASSEQ "$(bup ls graft-point-relative/latest/$TOP/grafted/)" "a
+b
+d/
+f"
+
WVSTART "indexfile"
D=indexfile.tmp
INDEXFILE=tmpindexfile.tmp