diff --git a/btv b/btv index 43570ad..99549ed 100755 --- a/btv +++ b/btv @@ -9,6 +9,7 @@ import json import os import shlex import sys +import time CONFIG = "/etc/btv/config.ini" LOCKFILE = "/run/lock/btv/serialization.lock" @@ -487,22 +488,41 @@ def do_stream(args): def do_gc(args=None): """ Drops old snapshots. + + If the only arg is "greedy", drops ALL snapshots except the youngest + Rank 2. """ - counts = [0, 0, 0] # Rank 0, 1, 2 - limits = [ - cfg.getint("gc", "rank_0_count"), - cfg.getint("gc", "rank_1_count"), - cfg.getint("gc", "rank_2_count") - ] - - for snap in reversed(list_snapshots()): - counts[snap.rank] += 1 + if args and "greedy" in args: + newest = list_snapshots(2)[-1] - if counts[snap.rank] > limits[snap.rank]: - print(" >> delete Rank %d %s" %(snap.rank, snap.name)) + if newest: + print(">>> Dropping all snapshots except the newest Rank 2 in 5 s...") + time.sleep(5) - snap.drop() + for snap in list_snapshots(): + if snap.name != newest.name: + snap.drop() + + else: + print("!!! no Rank 2 snapshot exists") + sys.exit(1) + + else: + counts = [0, 0, 0] # Rank 0, 1, 2 + limits = [ + cfg.getint("gc", "rank_0_count"), + cfg.getint("gc", "rank_1_count"), + cfg.getint("gc", "rank_2_count") + ] + + for snap in reversed(list_snapshots()): + counts[snap.rank] += 1 + + if counts[snap.rank] > limits[snap.rank]: + print(" >> delete Rank %d %s" %(snap.rank, snap.name)) + + snap.drop() verb_router = { "snapshot": do_create, @@ -537,6 +557,9 @@ Verbs: gc Drops old local snapshots based on garbage collector settings. + + gc greedy + Drop ALL snapshots except the newest Rank 2. """.format(cmd=sys.argv[0])) if __name__ == "__main__":