Commit 3b375000 authored by Robert Spencer's avatar Robert Spencer
Browse files

Rewite backstep-traceback-auto in python



In anticipation of future changes.
Signed-off-by: Robert Spencer's avatarRobert Spencer <rspencer@lbsd.net>
parent f5aad4cb
# Example configuration file for use by backstep-traceback-auto # Example configuration file for use by backstep-traceback-auto
# #
# Edit as need and save to /etc/backstep-traceback/daily.conf for the # Edit as need and save to: /etc/backstep-traceback/
# daily backup and /etc/backstep-traceback/weekly.conf for the weekly
# backup.
# Config [DEFAULT]
BWLIMIT= BWLIMIT=
DEBUG_MAX_LOAD= DEBUG_MAX_LOAD=
MAX_LOAD= MAX_LOAD=
REPORT_EMAIL= REPORT_EMAIL=
ZABBIX_CONFIG= ZABBIX_CONFIG=
# Server details
SERVER_HOST=
SERVER_PATH=backup SERVER_PATH=backup
SERVER_PORT=
SERVER_KEY=/root/.ssh/backupserver_id_rsa SERVER_KEY=/root/.ssh/backupserver_id_rsa
SERVER_USER= SERVER_USER=
# Logical volumes to backup # Logical volumes to backup
BACKUP_LVS=() BACKUP_LVS=[
BACKUP_LVS+=("example.com") "example.com"]
[daily]
SERVER_HOST=
SERVER_PORT=
[weekly]
SERVER_HOST=
SERVER_PORT=
#!/bin/bash #!/usr/bin/python3
# backstep-traceback-auto - Automatic backup script using backstep-traceback """
# Copyright (c) 2010-2017, AllWorldIT 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 This program is free software: you can redistribute it and/or modify
# the Free Software Foundation, either version 3 of the License, or it under the terms of the GNU General Public License as published by
# (at your option) any later version. 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 This program is distributed in the hope that it will be useful,
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the but WITHOUT ANY WARRANTY; without even the implied warranty of
# GNU General Public License for more details. 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 <http://www.gnu.org/licenses/>. You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
#set -e -x
"""
function show_help {
cat <<-EOF import argparse
$0 [-hv] [-o lv-name] [daily|weekly] import ast
import configparser
-h Show this help. import os
-v Show verbose debug information & set debug options. import subprocess
-o Only do a backup of the specified lv. import sys
EOF parser = argparse.ArgumentParser(
} description="Automatic backup script using backstep-traceback."
)
export PATH="$PATH:/sbin:/usr/sbin" parser.add_argument(
"-v",
# Parse options "--verbose",
OPTIND=1 action="store_true",
while getopts ":ho:v" OPTION help="Show verbose debug information & set debug options",
do )
case "$OPTION" in group = parser.add_mutually_exclusive_group()
h) group.add_argument("-d", "--daily", action="store_true", help="Do daily backups")
show_help group.add_argument("-w", "--weekly", action="store_true", help="Do weekly backups")
exit 0 parser.add_argument(
;; "-o", "--only", help="Only do a backup of the specified lv", metavar="lv_name"
o) )
ONLY="$OPTARG" args = parser.parse_args()
;;
v) if args.daily:
VERBOSE="true" PERIOD = "daily"
;; elif args.weekly:
esac PERIOD = "weekly"
done else:
# Remove option from parameters print("error: You need to specify a period [daily or weekly].")
shift $(expr $OPTIND - 1) sys.exit(2)
if [ -n "$1" ] CONFIG_FILE = "/etc/backstep-traceback/backstep-traceback-auto.ini"
then
PERIOD="$1" if not os.path.exists(CONFIG_FILE):
else print("error: backup is not configured.")
echo "ERROR: You need to specify a period [daily or weekly]." sys.exit(2)
exit 1
fi config = configparser.ConfigParser()
config.read(CONFIG_FILE)
if [ -e /etc/backstep-traceback/${PERIOD}.conf ]
then bwlimit = config.get(PERIOD, "bwlimit")
source /etc/backstep-traceback/${PERIOD}.conf debug_max_load = config.get(PERIOD, "debug_max_load")
else max_load = config.get(PERIOD, "max_load")
echo "ERROR: ${PERIOD^} backup is not configured." report_email = config.get(PERIOD, "report_email")
exit 1 server_host = config.get(PERIOD, "server_host")
fi server_key = config.get(PERIOD, "server_key")
server_path = config.get(PERIOD, "server_path")
# Change sync options for when we are debugging server_port = config.get(PERIOD, "server_port")
if [ "$VERBOSE" = "true" ] server_user = config.get(PERIOD, "server_user")
then zabbix_config = config.get(PERIOD, "zabbix_config")
syncopts="--debug --max-load=$DEBUG_MAX_LOAD"
set -ex if args.verbose:
else syncopts = ["--debug", "--max-load=" + debug_max_load]
syncopts="--max-load=$MAX_LOAD --quiet --bwlimit=$BWLIMIT" else:
fi syncopts = ["--max-load=" + max_load, "--quiet", "--bwlimit=" + bwlimit]
# Loop with all LVS for lv in ast.literal_eval(config.get(PERIOD, "backup_lvs")):
for LV in "${BACKUP_LVS[@]}" if args.only and args.only != lv:
do continue
if [ -n "$ONLY" ] && [ "$ONLY" != "$LV" ]
then bind_mount_args = []
continue lvm_snapshot_args = []
fi VG = "lvm-raid"
OLD_IFS="$IFS" if "," in lv:
IFS=',' lv, bind_mount = lv.split(",")
bind_mount_args.append("--bind-mount")
read -a ITEMS -r <<< "$LV" bind_mount_args.append(bind_mount + ":" + bind_mount)
IFS="$OLD_IFS" lvm_snapshot_args.append("--lvm-snapshot")
BIND_MOUNT_ARGS=() lvm_snapshot_args.append(VG + "/" + lv)
LVM_SNAPSHOT_ARGS=()
if len(lvm_snapshot_args) > 2:
for ITEM in "${ITEMS[@]}" ZABBIX_ITEM = PERIOD
do else:
case "$ITEM" in ZABBIX_ITEM = lv + "," + PERIOD
/*)
BIND_MOUNT_ARGS+=("--bind-mount") command_line = [
BIND_MOUNT_ARGS+=("$ITEM:$ITEM") "backstep-traceback",
;; "--backup",
*) "--acl",
LVM_SNAPSHOT_ARGS+=("--lvm-snapshot") "--safe",
LVM_SNAPSHOT_ARGS+=("lvm-raid/$ITEM") "--deltas",
;; "--create-timestamp-file",
esac "--copy-backup-filter",
done "--prepend-backup-filter",
]
lv=${LVM_SNAPSHOT_ARGS[1]#*/} command_line.extend(lvm_snapshot_args)
vg=${LVM_SNAPSHOT_ARGS[1]%/*} command_line.extend(bind_mount_args)
command_line.append('--email-subject="' + PERIOD.upper() + " " + lv + '"')
if [ "${#LVM_SNAPSHOT_ARGS[@]}" -gt "2" ] command_line.append("--email=" + report_email)
then command_line.extend(["--port=" + server_port, "--pubkey=" + server_key])
ZABBIX_ITEM="$PERIOD" command_line.extend(syncopts)
else command_line.extend(["--zabbix=" + ZABBIX_ITEM, "--zabbix-config=" + zabbix_config])
ZABBIX_ITEM="$lv,$PERIOD" command_line.append(
fi server_user
+ "@"
# Backup to $PERIOD-$vg-$lv ... + server_host
backstep-traceback --backup \ + "::"
--acl \ + server_path
--safe \ + "/"
--deltas \ + PERIOD
--create-timestamp-file \ + "-"
--copy-backup-filter \ + VG
--prepend-backup-filter \ + "-"
"${LVM_SNAPSHOT_ARGS[@]}" \ + lv
"${BIND_MOUNT_ARGS[@]}" \ )
--email-subject "${PERIOD^^} $lv" \
--email="$REPORT_EMAIL" \ EXIT_CODE = subprocess.call(command_line)
--port "$SERVER_PORT" --pubkey "$SERVER_KEY" \
$syncopts \
--zabbix "$ZABBIX_ITEM" --zabbix-config "$ZABBIX_CONFIG" \
"$SERVER_USER"@"$SERVER_HOST"::"$SERVER_PATH"/"$PERIOD-$vg-$lv"
done
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment