#
# Utility for managing LBS archives.
-import getpass, os, sys
+import getpass, os, stat, sys, time
from optparse import OptionParser
import lbs
if not os.environ.has_key(ENV_KEY):
os.environ[ENV_KEY] = getpass.getpass()
+# Delete old snapshots from the local database, though do not actually schedule
+# any segment cleaning.
+# Syntax: $0 --localdb=LOCALDB prune-db
+def cmd_prune_db():
+ db = lbs.LocalDatabase(options.localdb)
+
+ # Delete old snapshots from the local database.
+ db.garbage_collect()
+ db.commit()
+
# Run the segment cleaner.
# Syntax: $0 --localdb=LOCALDB clean
def cmd_clean(clean_threshold=7.0):
previous = set(segments)
print "%s: %.3f +%.3f -%.3f" % (s, size / 1024.0**2, added / 1024.0**2, removed / 1024.0**2)
-# Build checksum list for objects in the given segments
+# Build checksum list for objects in the given segments, or all segments if
+# none are specified.
def cmd_object_checksums(segments):
+ get_passphrase()
lowlevel = lbs.LowlevelDataStore(options.store)
store = lbs.ObjectStore(lowlevel)
+ if len(segments) == 0:
+ segments = sorted(lowlevel.list_segments())
for s in segments:
for (o, data) in store.load_segment(s):
csum = lbs.ChecksumCreator().update(data).compute()
raise ValueError("Bad checksum found")
store.cleanup()
+# Restore a snapshot, or some subset of files from it
+def cmd_restore_snapshot(args):
+ get_passphrase()
+ lowlevel = lbs.LowlevelDataStore(options.store)
+ store = lbs.ObjectStore(lowlevel)
+ snapshot = lbs.parse_full(store.load_snapshot(args[0]))
+ destdir = args[1]
+ paths = args[2:]
+
+ def warn(m, msg):
+ print "Warning: %s: %s" % (m.items.name, msg)
+
+ for m in lbs.iterate_metadata(store, snapshot['Root']):
+ pathname = os.path.normpath(m.items.name)
+ while os.path.isabs(pathname):
+ pathname = pathname[1:]
+ print pathname
+ destpath = os.path.join(destdir, pathname)
+ (path, filename) = os.path.split(destpath)
+
+ # TODO: Check for ../../../paths that might attempt to write outside
+ # the destination directory. Maybe also check attempts to follow
+ # symlinks pointing outside?
+
+ try:
+ if not os.path.isdir(path):
+ os.makedirs(path)
+
+ if m.items.type == '-':
+ file = open(destpath, 'wb')
+ verifier = lbs.ChecksumVerifier(m.items.checksum)
+ size = 0
+ for block in m.data():
+ data = store.get(block)
+ verifier.update(data)
+ size += len(data)
+ file.write(data)
+ file.close()
+ if int(m.fields['size']) != size:
+ raise ValueError("File size does not match!")
+ if not verifier.valid():
+ raise ValueError("Bad checksum found")
+ elif m.items.type == 'd':
+ if filename != '.':
+ os.mkdir(destpath)
+ elif m.items.type == 'l':
+ os.symlink(m.items.contents, destpath)
+ elif m.items.type == 'p':
+ os.mkfifo(destpath)
+ elif m.items.type in ('c', 'b'):
+ if m.items.type == 'c':
+ mode = 0600 | stat.S_IFCHR
+ else:
+ mode = 0600 | stat.S_IFBLK
+ os.mknod(destpath, mode, os.makedev(*m.items.device))
+ elif m.items.type == 's':
+ pass # TODO: Implement
+ else:
+ warn(m, "Unknown type code: " + m.items.type)
+ continue
+
+ except Exception, e:
+ warn(m, "Error restoring: %s" % (e,))
+ continue
+
+ try:
+ uid = m.items.user[0]
+ gid = m.items.group[0]
+ os.lchown(destpath, uid, gid)
+ except Exception, e:
+ warn(m, "Error restoring file ownership: %s" % (e,))
+
+ if m.items.type == 'l':
+ continue
+
+ try:
+ os.chmod(destpath, m.items.mode)
+ except Exception, e:
+ warn(m, "Error restoring file permissions: %s" % (e,))
+
+ try:
+ os.utime(destpath, (time.time(), m.items.mtime))
+ except Exception, e:
+ warn(m, "Error restoring file timestamps: %s" % (e,))
+
+ store.cleanup()
+
if len(args) == 0:
parser.print_usage()
sys.exit(1)
args = args[1:]
if cmd == 'clean':
cmd_clean()
+elif cmd == 'prune-db':
+ cmd_prune_db()
elif cmd == 'list-snapshots':
cmd_list_snapshots()
elif cmd == 'object-sums':
cmd_list_snapshot_sizes()
elif cmd == 'verify-snapshots':
cmd_verify_snapshots(args)
+elif cmd == 'restore-snapshot':
+ cmd_restore_snapshot(args)
else:
print "Unknown command:", cmd
parser.print_usage()