From a20abdd71fce0a5968c8445f8eca3338e4759341 Mon Sep 17 00:00:00 2001 From: Paolo Cozzi Date: Tue, 8 Mar 2016 14:55:57 +0100 Subject: [PATCH] Filtering user defined domains. Copyright updated Now I can specify the domain to backup by command line. If such domain exists in config file, will be backup --- Lib/__init__.py | 2 +- Lib/flock.py | 2 +- Lib/helper.py | 2 +- README.md | 6 ++--- TODO | 1 + config.yml | 2 +- kvmBackup.py | 72 ++++++++++++++++++++++++++++++++++--------------- 7 files changed, 59 insertions(+), 28 deletions(-) diff --git a/Lib/__init__.py b/Lib/__init__.py index e157b43..a90b2d6 100644 --- a/Lib/__init__.py +++ b/Lib/__init__.py @@ -2,7 +2,7 @@ """ kvmBackup - a software for snapshotting KVM images and backing them up -Copyright (C) 2015 PTP +Copyright (C) 2015-2016 PTP This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/Lib/flock.py b/Lib/flock.py index c5724dd..3db2547 100644 --- a/Lib/flock.py +++ b/Lib/flock.py @@ -2,7 +2,7 @@ """ kvmBackup - a software for snapshotting KVM images and backing them up -Copyright (C) 2015 PTP +Copyright (C) 2015-2016 PTP This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/Lib/helper.py b/Lib/helper.py index e685a07..2dbbfe8 100644 --- a/Lib/helper.py +++ b/Lib/helper.py @@ -2,7 +2,7 @@ """ kvmBackup - a software for snapshotting KVM images and backing them up -Copyright (C) 2015 PTP +Copyright (C) 2015-2016 PTP This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/README.md b/README.md index ec7b4b6..f854355 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ found in our [Wiki](https://github.com/bioinformatics-ptp/kvmBackup/wiki) ## License kvmBackup - a software for snapshotting KVM images and backing them up -Copyright (C) 2015 PTP +Copyright (C) 2015-2016 PTP This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -90,7 +90,7 @@ cloud1: #The hostname which will do backup ``` The first level (`cloud1` in the example) is the name of the host in which those -configuration apply; it could be determined by typing `hostname -s`. Configurations +configurations apply; it could be determined by typing `hostname -s`. Configurations defined under a different hostname will not by applied in such host. The second yaml level define a `domain` by specyfing all Guest domains we need to provide a backup, and a `backupdir` directory in which backup will be placed. In this directory will @@ -105,7 +105,7 @@ More information on kvmBackup configuration could be found in our [wiki - Config ## Usage -Launch kvmBackup as a provileged user (`root` or using `suod`) by specyfing the +Launch kvmBackup as a provileged user (`root` or using `sudo`) by specyfing the path of your config file: ```bash diff --git a/TODO b/TODO index 7873a2b..2c5e0a9 100644 --- a/TODO +++ b/TODO @@ -4,3 +4,4 @@ * verbosity level * copy CD-rom data * testing wrong configurations +* Snapshotting only a defined domain diff --git a/config.yml b/config.yml index 9ea3dfb..d2a7938 100644 --- a/config.yml +++ b/config.yml @@ -1,6 +1,6 @@ # # kvmBackup - a software for snapshotting KVM images and backing them up -# Copyright (C) 2015 PTP +# Copyright (C) 2015-2016 PTP # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by diff --git a/kvmBackup.py b/kvmBackup.py index 40ba65c..24748c6 100755 --- a/kvmBackup.py +++ b/kvmBackup.py @@ -3,7 +3,7 @@ """ kvmBackup - a software for snapshotting KVM images and backing them up -Copyright (C) 2015 PTP +Copyright (C) 2015-2016 PTP This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -46,7 +46,7 @@ logger = logging.getLogger(prog_name) notice = """ -kvmBackup.py Copyright (C) 2015 PTP +kvmBackup.py Copyright (C) 2015-2016 PTP This program comes with ABSOLUTELY NO WARRANTY; for details type `kvmBackup.py --help'. This is free software, and you are welcome to redistribute it under certain conditions; see LICENSE.txt for details. @@ -79,16 +79,35 @@ def checkDay(day): return False +def filterDomains(domains, user_domains): + """filter domamin by user domains""" + + # Those are user domains (as a list) + user_domains = [domain.strip() for domain in user_domains.split(",")] + found_domains = [] + + # test for domain existances + for domain in user_domains: + if domain not in domains: + logger.error("User domain '%s' not found" %(domain)) + + else: + found_domains += [domain] + + # Now return the filtered domains + return found_domains + + def backup(domain, parameters, backupdir): """Do all the operation needed for backup""" - + #changing directory olddir = os.getcwd() workdir = os.path.join(backupdir, domain) #creating directory if not exists if not os.path.exists(workdir) and not os.path.isdir(workdir): - logger.info("Creating directory %s" %(workdir)) + logger.info("Creating directory '%s'" %(workdir)) os.mkdir(workdir) #cange directory @@ -99,7 +118,7 @@ def backup(domain, parameters, backupdir): datadir = os.path.join(workdir, date) #creating datadir - logger.debug("Creating directory %s" %(datadir)) + logger.debug("Creating directory '%s'" %(datadir)) os.mkdir(datadir) #define the target backup @@ -123,7 +142,7 @@ def backup(domain, parameters, backupdir): xml_files = snapshot.dumpXML(path=datadir) #Add xmlsto archive, and remove original file - logger.info('Adding XMLs files for domain %s to archive %s' %(domain, tar_path)) + logger.info("Adding XMLs files for domain '%s' to archive '%s'" %(domain, tar_path)) for xml_file in xml_files: #backup file with its relative path @@ -131,31 +150,31 @@ def backup(domain, parameters, backupdir): xml_file = os.path.join(date, xml_file) tar.add(xml_file) - logger.debug("%s added" %(xml_file)) + logger.debug("'%s' added" %(xml_file)) - logger.debug("removing %s from %s" %(xml_file, datadir)) + logger.debug("removing '%s' from '%s'" %(xml_file, datadir)) os.remove(xml_file) #call snapshot snapshot.callSnapshot() - logger.info('Adding image files for %s to archive %s' %(domain, tar_path)) + logger.info("Adding image files for '%s' to archive %'s'" %(domain, tar_path)) #copying file for disk, source in snapshot.disks.iteritems(): dest = os.path.join(datadir, os.path.basename(source)) - logger.debug("copying %s to %s" %(source, dest)) + logger.debug("copying '%s' to '%s'" %(source, dest)) shutil.copy2( source, dest ) #backup file with its relative path img_file = os.path.basename(dest) img_file = os.path.join(date, img_file) - logger.debug("Adding %s to archive %s" %(img_file, tar_path)) + logger.debug("Adding '%s' to archive '%s'" %(img_file, tar_path)) tar.add(img_file) - logger.debug("removing %s from %s" %(img_file, datadir)) + logger.debug("removing '%s' from '%s'" %(img_file, datadir)) os.remove(img_file) #block commit (and delete snapshot) @@ -165,17 +184,17 @@ def backup(domain, parameters, backupdir): tar.close() #Now launcing subprocess with pigz - logger.info("Compressing %s" %(tar_name)) + logger.info("Compressing '%s'" %(tar_name)) helper.packArchive(target=tar_name) #revoving EMPTY datadir - logger.debug("removing %s" %(datadir)) + logger.debug("removing '%s'" %(datadir)) os.rmdir(datadir) #return to the original directory os.chdir(olddir) - logger.info("Backup for %s completed" %(domain)) + logger.info("Backup for '%s' completed" %(domain)) # A global connection instance @@ -185,6 +204,7 @@ def backup(domain, parameters, backupdir): parser = argparse.ArgumentParser(description='Backup of KVM-qcow2 domains') parser.add_argument("-c", "--config", required=True, type=str, help="The config file") parser.add_argument("--force", required=False, action='store_true', default=False, help="Force backup (with rotation)") + parser.add_argument("--domains", required=False, type=str, help="comma separated list of domains to backup ('virsh list --all' to get domains)") args = parser.parse_args() #logging notice @@ -192,7 +212,7 @@ def backup(domain, parameters, backupdir): sys.stderr.flush() #Starting software - logger.info("Starting %s" %(prog_name)) + logger.info("Starting '%s'" %(prog_name)) lockfile = os.path.splitext(os.path.basename(sys.argv[0]))[0] + ".lock" lockfile_path = os.path.join("/var/run", lockfile) @@ -200,18 +220,23 @@ def backup(domain, parameters, backupdir): lock = flock.flock(lockfile_path, True).acquire() if not lock: - logger.error("Another istance of %s is running. Please wait for its termination or kill the running application" %(sys.argv[0])) + logger.error("Another istance of '%s' is running. Please wait for its termination or kill the running application" %(sys.argv[0])) sys.exit(-1) #get all domain names domains = [domain.name() for domain in conn.listAllDomains()] + + # filter domains with user provides domains (if needed) + if args.domains is not None: + logger.info("Checking '%s' domains" %(args.domains)) + domains = filterDomains(domains,args.domains) #parse configuration file mydomains, backupdir, config = loadConf(args.config) #test for directory existance if not os.path.exists(backupdir) and os.path.isdir(backupdir) is False: - logger.info("Creating directory %s" %(backupdir)) + logger.info("Creating directory '%s'" %(backupdir)) os.mkdir(backupdir) #debug @@ -220,10 +245,15 @@ def backup(domain, parameters, backupdir): for domain_name, parameters in mydomains.iteritems(): #check if bakcup is needed domain_backup = False + + #check if configuration domain exists or was filtered out + if domain_name not in domains: + logger.warn("Ignoring domain '%s'" %(domain_name)) + continue for day in parameters["day_of_week"]: if checkDay(day) is True or args.force is True: - logger.info("Ready for backup of %s" %(domain_name)) + logger.info("Ready for backup of '%s'" %(domain_name)) domain_backup = True #do backup stuff @@ -233,7 +263,7 @@ def backup(domain, parameters, backupdir): break if domain_backup is False: - logger.debug("Ignoring %s domain" %(domain_name)) + logger.debug("Ignoring '%s' domain" %(domain_name)) #end of the program - logger.info("%s completed successfully" %(prog_name)) + logger.info("'%s' completed successfully" %(prog_name))