From 3b375000a1b8307c7459d916cbaee8b50e6d2891 Mon Sep 17 00:00:00 2001 From: Robert Spencer Date: Tue, 31 Aug 2021 17:00:34 +0200 Subject: [PATCH] Rewite backstep-traceback-auto in python In anticipation of future changes. Signed-off-by: Robert Spencer --- ...-auto.conf => backstep-traceback-auto.ini} | 21 +- scripts/backstep-traceback-auto | 265 +++++++++--------- 2 files changed, 142 insertions(+), 144 deletions(-) rename example/{backstep-traceback-auto.conf => backstep-traceback-auto.ini} (54%) mode change 100755 => 100644 scripts/backstep-traceback-auto diff --git a/example/backstep-traceback-auto.conf b/example/backstep-traceback-auto.ini similarity index 54% rename from example/backstep-traceback-auto.conf rename to example/backstep-traceback-auto.ini index c866a87..29f4cd8 100644 --- a/example/backstep-traceback-auto.conf +++ b/example/backstep-traceback-auto.ini @@ -1,23 +1,26 @@ # Example configuration file for use by backstep-traceback-auto # -# Edit as need and save to /etc/backstep-traceback/daily.conf for the -# daily backup and /etc/backstep-traceback/weekly.conf for the weekly -# backup. +# Edit as need and save to: /etc/backstep-traceback/ -# Config +[DEFAULT] BWLIMIT= DEBUG_MAX_LOAD= MAX_LOAD= REPORT_EMAIL= ZABBIX_CONFIG= -# Server details -SERVER_HOST= SERVER_PATH=backup -SERVER_PORT= SERVER_KEY=/root/.ssh/backupserver_id_rsa SERVER_USER= # Logical volumes to backup -BACKUP_LVS=() -BACKUP_LVS+=("example.com") +BACKUP_LVS=[ + "example.com"] + +[daily] +SERVER_HOST= +SERVER_PORT= + +[weekly] +SERVER_HOST= +SERVER_PORT= diff --git a/scripts/backstep-traceback-auto b/scripts/backstep-traceback-auto old mode 100755 new mode 100644 index a75beb6..745a447 --- a/scripts/backstep-traceback-auto +++ b/scripts/backstep-traceback-auto @@ -1,135 +1,130 @@ -#!/bin/bash -# backstep-traceback-auto - Automatic backup script using backstep-traceback -# Copyright (c) 2010-2017, AllWorldIT -# -# 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 -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -#set -e -x - -function show_help { - cat <<-EOF - $0 [-hv] [-o lv-name] [daily|weekly] - - -h Show this help. - -v Show verbose debug information & set debug options. - -o Only do a backup of the specified lv. - -EOF -} - -export PATH="$PATH:/sbin:/usr/sbin" - -# Parse options -OPTIND=1 -while getopts ":ho:v" OPTION -do - case "$OPTION" in - h) - show_help - exit 0 - ;; - o) - ONLY="$OPTARG" - ;; - v) - VERBOSE="true" - ;; - esac -done -# Remove option from parameters -shift $(expr $OPTIND - 1) - -if [ -n "$1" ] -then - PERIOD="$1" -else - echo "ERROR: You need to specify a period [daily or weekly]." - exit 1 -fi - -if [ -e /etc/backstep-traceback/${PERIOD}.conf ] -then - source /etc/backstep-traceback/${PERIOD}.conf -else - echo "ERROR: ${PERIOD^} backup is not configured." - exit 1 -fi - -# Change sync options for when we are debugging -if [ "$VERBOSE" = "true" ] -then - syncopts="--debug --max-load=$DEBUG_MAX_LOAD" - set -ex -else - syncopts="--max-load=$MAX_LOAD --quiet --bwlimit=$BWLIMIT" -fi - -# Loop with all LVS -for LV in "${BACKUP_LVS[@]}" -do - if [ -n "$ONLY" ] && [ "$ONLY" != "$LV" ] - then - continue - fi - - OLD_IFS="$IFS" - IFS=',' - - read -a ITEMS -r <<< "$LV" - - IFS="$OLD_IFS" - BIND_MOUNT_ARGS=() - LVM_SNAPSHOT_ARGS=() - - for ITEM in "${ITEMS[@]}" - do - case "$ITEM" in - /*) - BIND_MOUNT_ARGS+=("--bind-mount") - BIND_MOUNT_ARGS+=("$ITEM:$ITEM") - ;; - *) - LVM_SNAPSHOT_ARGS+=("--lvm-snapshot") - LVM_SNAPSHOT_ARGS+=("lvm-raid/$ITEM") - ;; - esac - done - - lv=${LVM_SNAPSHOT_ARGS[1]#*/} - vg=${LVM_SNAPSHOT_ARGS[1]%/*} - - if [ "${#LVM_SNAPSHOT_ARGS[@]}" -gt "2" ] - then - ZABBIX_ITEM="$PERIOD" - else - ZABBIX_ITEM="$lv,$PERIOD" - fi - - # Backup to $PERIOD-$vg-$lv ... - backstep-traceback --backup \ - --acl \ - --safe \ - --deltas \ - --create-timestamp-file \ - --copy-backup-filter \ - --prepend-backup-filter \ - "${LVM_SNAPSHOT_ARGS[@]}" \ - "${BIND_MOUNT_ARGS[@]}" \ - --email-subject "${PERIOD^^} $lv" \ - --email="$REPORT_EMAIL" \ - --port "$SERVER_PORT" --pubkey "$SERVER_KEY" \ - $syncopts \ - --zabbix "$ZABBIX_ITEM" --zabbix-config "$ZABBIX_CONFIG" \ - "$SERVER_USER"@"$SERVER_HOST"::"$SERVER_PATH"/"$PERIOD-$vg-$lv" -done +#!/usr/bin/python3 +""" +backstep-traceback-auto - Automatic backup script using backstep-traceback +Copyright (c) 2010-2021, AllWorldIT + +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 +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . + +""" + +import argparse +import ast +import configparser +import os +import subprocess +import sys + +parser = argparse.ArgumentParser( + description="Automatic backup script using backstep-traceback." +) +parser.add_argument( + "-v", + "--verbose", + action="store_true", + help="Show verbose debug information & set debug options", +) +group = parser.add_mutually_exclusive_group() +group.add_argument("-d", "--daily", action="store_true", help="Do daily backups") +group.add_argument("-w", "--weekly", action="store_true", help="Do weekly backups") +parser.add_argument( + "-o", "--only", help="Only do a backup of the specified lv", metavar="lv_name" +) +args = parser.parse_args() + +if args.daily: + PERIOD = "daily" +elif args.weekly: + PERIOD = "weekly" +else: + print("error: You need to specify a period [daily or weekly].") + sys.exit(2) + +CONFIG_FILE = "/etc/backstep-traceback/backstep-traceback-auto.ini" + +if not os.path.exists(CONFIG_FILE): + print("error: backup is not configured.") + sys.exit(2) + +config = configparser.ConfigParser() +config.read(CONFIG_FILE) + +bwlimit = config.get(PERIOD, "bwlimit") +debug_max_load = config.get(PERIOD, "debug_max_load") +max_load = config.get(PERIOD, "max_load") +report_email = config.get(PERIOD, "report_email") +server_host = config.get(PERIOD, "server_host") +server_key = config.get(PERIOD, "server_key") +server_path = config.get(PERIOD, "server_path") +server_port = config.get(PERIOD, "server_port") +server_user = config.get(PERIOD, "server_user") +zabbix_config = config.get(PERIOD, "zabbix_config") + +if args.verbose: + syncopts = ["--debug", "--max-load=" + debug_max_load] +else: + syncopts = ["--max-load=" + max_load, "--quiet", "--bwlimit=" + bwlimit] + +for lv in ast.literal_eval(config.get(PERIOD, "backup_lvs")): + if args.only and args.only != lv: + continue + + bind_mount_args = [] + lvm_snapshot_args = [] + VG = "lvm-raid" + + if "," in lv: + lv, bind_mount = lv.split(",") + bind_mount_args.append("--bind-mount") + bind_mount_args.append(bind_mount + ":" + bind_mount) + + lvm_snapshot_args.append("--lvm-snapshot") + lvm_snapshot_args.append(VG + "/" + lv) + + if len(lvm_snapshot_args) > 2: + ZABBIX_ITEM = PERIOD + else: + ZABBIX_ITEM = lv + "," + PERIOD + + command_line = [ + "backstep-traceback", + "--backup", + "--acl", + "--safe", + "--deltas", + "--create-timestamp-file", + "--copy-backup-filter", + "--prepend-backup-filter", + ] + command_line.extend(lvm_snapshot_args) + command_line.extend(bind_mount_args) + command_line.append('--email-subject="' + PERIOD.upper() + " " + lv + '"') + command_line.append("--email=" + report_email) + command_line.extend(["--port=" + server_port, "--pubkey=" + server_key]) + command_line.extend(syncopts) + command_line.extend(["--zabbix=" + ZABBIX_ITEM, "--zabbix-config=" + zabbix_config]) + command_line.append( + server_user + + "@" + + server_host + + "::" + + server_path + + "/" + + PERIOD + + "-" + + VG + + "-" + + lv + ) + + EXIT_CODE = subprocess.call(command_line) -- GitLab