X-Git-Url: https://git.adam-barratt.org.uk/?a=blobdiff_plain;f=modules%2Fbacula%2Ffiles%2Fvolumes-delete-old;h=80f2c935bbc7ef5f4304f2a0b5ba336df0031b35;hb=891ab115f249a1c7e186cca9145989112353a5a0;hp=cc0f92e9f40d0f3a6ae9fe851cb40f2bf6547134;hpb=194096396ce22744e79f307ed2723034b97a5ecb;p=mirror%2Fdsa-puppet.git diff --git a/modules/bacula/files/volumes-delete-old b/modules/bacula/files/volumes-delete-old index cc0f92e9f..80f2c935b 100755 --- a/modules/bacula/files/volumes-delete-old +++ b/modules/bacula/files/volumes-delete-old @@ -2,7 +2,7 @@ # queries a bacula database for volumes to delete and deletes them using bconsole -# Copyright 2010, 2011, 2013, 2017 Peter Palfrader +# Copyright 2010, 2011, 2013, 2017, 2018 Peter Palfrader # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the @@ -24,18 +24,25 @@ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. import argparse +import os.path import psycopg2 import psycopg2.extras import re import sys import subprocess +DSA_BACULA_DB_CONNECT = '/etc/dsa/bacula-reader-database' +DSA_CLIENT_LIST_FILE = '/etc/bacula/dsa-clients' + parser = argparse.ArgumentParser() parser.add_argument("-d", "--db-connect-string", metavar="connect-string", dest="db", help="Database connect string") parser.add_argument("-D", "--db-connect-string-file", metavar="FILE", dest="dbfile", - default='/etc/dsa/bacula-reader-database', - help="File to read database connect string from (/etc/dsa/bacula-reader-database)") + default=DSA_BACULA_DB_CONNECT, + help="File to read database connect string from (%s)"%(DSA_BACULA_DB_CONNECT,)) +parser.add_argument("-c", "--client-list", metavar="FILE", dest="clientlist", + default=DSA_CLIENT_LIST_FILE, + help="File with a list of all clients (%s)"%(DSA_CLIENT_LIST_FILE,)) parser.add_argument("-v", "--verbose", dest="verbose", default=False, action="store_true", help="Be more verbose.") @@ -107,20 +114,33 @@ for r in cursor.fetchall(): c = "delete volume=%s yes"%(r['volumename'],) cmd.append(c) -# find obsolete pools -cursor.execute(""" - SELECT name - FROM pool - WHERE - name != 'Scratch' AND - numvols = 0 AND - poolid NOT IN (SELECT recyclepoolid FROM media) -""", {}) - -for r in cursor.fetchall(): - c = "delete pool=%s"%(r['name'],) - cmd.append(c) - cmd.append("yes") +# find obsolete pools, but only if we have a list of clients +## +if os.path.exists(args.clientlist): + clients = set(open(args.clientlist).read().split()) + + cursor.execute(""" + SELECT name + FROM pool + WHERE + name != 'Scratch' AND + numvols = 0 AND + poolid NOT IN (SELECT recyclepoolid FROM media) + """, {}) + + for r in cursor.fetchall(): + poolname = r['name'] + match = re.match('pool[a-z]*-debian-(.*)', poolname) + if match is not None: + hostname = match.group(1) + if hostname not in clients: + c = "delete pool=%s"%(poolname,) + cmd.append(c) + cmd.append("yes") + elif args.verbose: + print("Not expiring empty pool %s because client still exists"%(poolname,)) + elif args.verbose: + print("Could not extract client name from poolname %s"%(poolname,)) if args.nodo: print("\n".join(cmd))