Skip to content

Commit

Permalink
Adds a strip and strip-path option to bup save.
Browse files Browse the repository at this point in the history
If the strip option is given bup uses all given filenames as base paths
and tries to stripe them from long to short.

If the strip-path option is given bup strip the given prefix from all
paths.

Signed-off-by: Zoran Zaric <zz@zoranzaric.de>
  • Loading branch information
zoranzaric authored and apenwarr committed Jan 3, 2011
1 parent 376d069 commit 0025c8d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
13 changes: 12 additions & 1 deletion cmd/save-cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
q,quiet don't show progress meter
smaller= only back up files smaller than n bytes
bwlimit= maximum bytes/sec to transmit to server
strip strips the path to every filename given
strip-path= path-prefix to be stripped when saving
"""
o = options.Options('bup save', optspec)
(opt, flags, extra) = o.parse(sys.argv[1:])
Expand All @@ -36,6 +38,9 @@
else:
date = time.time()

if opt.strip and opt.strip_path:
o.fatal("--strip is incompatible with --strip-path")

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")
Expand Down Expand Up @@ -195,7 +200,13 @@ def wantrecurse_during(ent):
continue

assert(dir.startswith('/'))
dirp = dir.split('/')
if opt.strip:
stripped_base_path = strip_base_path(dir, extra)
dirp = stripped_base_path.split('/')
elif opt.strip_path:
dirp = strip_path(opt.strip_path, dir).split('/')
else:
dirp = dir.split('/')
while parts > dirp:
_pop(force_tree = None)
if dir != '/':
Expand Down
33 changes: 33 additions & 0 deletions lib/bup/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,39 @@ def parse_date_or_fatal(str, fatal):
else:
return date

def strip_path(prefix, path):
"""Strips a given prefix from a path.
First both paths are normalized.
Raises an Exception if no prefix is given.
"""
if prefix == None:
raise Exception('no path given')

normalized_prefix = realpath(prefix)
print "normalized_prefix: " + normalized_prefix
normalized_path = realpath(path)
print "normalized_path: " + normalized_path
if normalized_path.startswith(normalized_prefix):
return normalized_path[len(normalized_prefix):]
else:
return path

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
a too short base_path is removed.
"""
sorted_base_paths = sorted(base_paths, key=len, reverse=True)
for bp in sorted_base_paths:
if path.startswith(realpath(bp)):
return strip_path(bp, path)
return 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
Expand Down
18 changes: 18 additions & 0 deletions lib/bup/t/thelpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,21 @@ def test_parse_num():
WVPASSEQ(pn('2 gb'), 2*1024*1024*1024)
WVPASSEQ(pn('1e+9 k'), 1000000000 * 1024)
WVPASSEQ(pn('-3e-3mb'), int(-0.003 * 1024 * 1024))

@wvtest
def test_strip_path():
prefix = "/var/backup/daily.0/localhost"
empty_prefix = ""
non_matching_prefix = "/home"
path = "/var/backup/daily.0/localhost/etc/"

WVPASSEQ(strip_path(prefix, path), '/etc')
WVPASSEQ(strip_path(empty_prefix, path), path)
WVPASSEQ(strip_path(non_matching_prefix, path), path)
WVEXCEPT(Exception, strip_path, None, path)

@wvtest
def test_strip_base_path():
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')

0 comments on commit 0025c8d

Please sign in to comment.