Skip to content

Commit

Permalink
✨ deal with wrong configuration files and add verbosity
Browse files Browse the repository at this point in the history
  • Loading branch information
bunop committed Feb 8, 2022
1 parent 7b82a04 commit efc5388
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
3 changes: 0 additions & 3 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@

* e-mail notify for problems
* add documentation
* verbosity level
* copy CD-rom data
* testing wrong configurations
* rotating using dates, not numbers
* dealing with power off VM
* deal with empty configurations
* python packaging?
41 changes: 36 additions & 5 deletions kvmBackup.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
# Logging istance
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.DEBUG)
level=logging.INFO)
logger = logging.getLogger(prog_name)

notice = """
Expand All @@ -65,15 +65,39 @@ def loadConf(file_conf):

config = yaml.load(open(file_conf))

if not config:
raise RuntimeError(
"Error in configuration file. Check for kvmbackup documentation")

# read my defined domains
hostname = socket.gethostname()
hostname = hostname.split(".")[0]

# try to parse useful data
mydomains = config[hostname]["domains"]
mydomains = {}
backupdir = None

if hostname not in config:
raise RuntimeError("Can't find '%s' in configuration file" % hostname)

try:
mydomains = config[hostname]["domains"]

except (TypeError, KeyError):
logger.warning("Cannot read 'domains' for '%s'" % hostname)
logger.warning("Is configuration file defined properly?")

# get my backup directory
backupdir = config[hostname]["backupdir"]
try:
backupdir = config[hostname]["backupdir"]

except (TypeError, KeyError):
logger.warning("Cannot read 'backupdir' for '%s'" % hostname)
logger.warning("Is configuration file defined properly?")

if not mydomains or not backupdir:
raise RuntimeError(
"Error in configuration file. Check for kvmbackup documentation")

return mydomains, backupdir, config

Expand Down Expand Up @@ -229,6 +253,7 @@ def backup(domain, parameters, backupdir):
# A global connection instance
conn = libvirt.open("qemu:///system")


if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Backup of KVM-qcow2 domains')
parser.add_argument("-c", "--config", required=True,
Expand All @@ -239,8 +264,14 @@ def backup(domain, parameters, backupdir):
"--domains", required=False, type=str,
help=("comma separated list of domains to backup ('virsh list "
"--all' to get domains)"))
parser.add_argument(
"-v", "--verbose", action='store_true',
help="verbose logging")
args = parser.parse_args()

if args.verbose:
logger.setLevel(logging.DEBUG)

# logging notice
sys.stderr.write(notice)
sys.stderr.flush()
Expand Down Expand Up @@ -287,7 +318,7 @@ def backup(domain, parameters, backupdir):

# check if configuration domain exists or was filtered out
if domain_name not in domains:
logger.warn("Ignoring domain '%s'" % (domain_name))
logger.info("Ignoring domain '%s'" % (domain_name))
continue

for day in parameters["day_of_week"]:
Expand All @@ -309,7 +340,7 @@ def backup(domain, parameters, backupdir):
break

if domain_backup is False:
logger.debug("Ignoring '%s' domain" % (domain_name))
logger.info("Ignoring '%s' domain" % (domain_name))

# end of the program
if flag_errors is False:
Expand Down

0 comments on commit efc5388

Please sign in to comment.