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
if [ -n "$ONLY" ] && [ "$ONLY" != "$LV" ]
then
continue continue
fi
bind_mount_args = []
OLD_IFS="$IFS" lvm_snapshot_args = []
IFS=',' VG = "lvm-raid"
read -a ITEMS -r <<< "$LV" if "," in lv:
lv, bind_mount = lv.split(",")
IFS="$OLD_IFS" bind_mount_args.append("--bind-mount")
BIND_MOUNT_ARGS=() bind_mount_args.append(bind_mount + ":" + bind_mount)
LVM_SNAPSHOT_ARGS=()
lvm_snapshot_args.append("--lvm-snapshot")
for ITEM in "${ITEMS[@]}" lvm_snapshot_args.append(VG + "/" + lv)
do
case "$ITEM" in if len(lvm_snapshot_args) > 2:
/*) ZABBIX_ITEM = PERIOD
BIND_MOUNT_ARGS+=("--bind-mount") else:
BIND_MOUNT_ARGS+=("$ITEM:$ITEM") ZABBIX_ITEM = lv + "," + PERIOD
;;
*) command_line = [
LVM_SNAPSHOT_ARGS+=("--lvm-snapshot") "backstep-traceback",
LVM_SNAPSHOT_ARGS+=("lvm-raid/$ITEM") "--backup",
;; "--acl",
esac "--safe",
done "--deltas",
"--create-timestamp-file",
lv=${LVM_SNAPSHOT_ARGS[1]#*/} "--copy-backup-filter",
vg=${LVM_SNAPSHOT_ARGS[1]%/*} "--prepend-backup-filter",
]
if [ "${#LVM_SNAPSHOT_ARGS[@]}" -gt "2" ] command_line.extend(lvm_snapshot_args)
then command_line.extend(bind_mount_args)
ZABBIX_ITEM="$PERIOD" command_line.append('--email-subject="' + PERIOD.upper() + " " + lv + '"')
else command_line.append("--email=" + report_email)
ZABBIX_ITEM="$lv,$PERIOD" command_line.extend(["--port=" + server_port, "--pubkey=" + server_key])
fi command_line.extend(syncopts)
command_line.extend(["--zabbix=" + ZABBIX_ITEM, "--zabbix-config=" + zabbix_config])
# Backup to $PERIOD-$vg-$lv ... command_line.append(
backstep-traceback --backup \ server_user
--acl \ + "@"
--safe \ + server_host
--deltas \ + "::"
--create-timestamp-file \ + server_path
--copy-backup-filter \ + "/"
--prepend-backup-filter \ + PERIOD
"${LVM_SNAPSHOT_ARGS[@]}" \ + "-"
"${BIND_MOUNT_ARGS[@]}" \ + VG
--email-subject "${PERIOD^^} $lv" \ + "-"
--email="$REPORT_EMAIL" \ + lv
--port "$SERVER_PORT" --pubkey "$SERVER_KEY" \ )
$syncopts \
--zabbix "$ZABBIX_ITEM" --zabbix-config "$ZABBIX_CONFIG" \ EXIT_CODE = subprocess.call(command_line)
"$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