backstep-traceback-auto 3.84 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/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 <http://www.gnu.org/licenses/>.

"""

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)