Commit f0807aa6 authored by Robert Spencer's avatar Robert Spencer
Browse files

Add failed and list options



backstep-traceback-auto will now cache a list of failed backups. As
a result I can now add an option to list the lv's that have not been
backed up and an option to back them up.
Signed-off-by: Robert Spencer's avatarRobert Spencer <rspencer@lbsd.net>
parent 8962ca0e
...@@ -21,6 +21,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. ...@@ -21,6 +21,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import argparse import argparse
import ast import ast
import configparser import configparser
from datetime import datetime
import json
import os import os
import subprocess import subprocess
import sys import sys
...@@ -37,7 +39,14 @@ parser.add_argument( ...@@ -37,7 +39,14 @@ parser.add_argument(
) )
group = parser.add_mutually_exclusive_group() group = parser.add_mutually_exclusive_group()
group.add_argument("-d", "--daily", action="store_true", help="Do daily backups") group.add_argument("-d", "--daily", action="store_true", help="Do daily backups")
group.add_argument("-f", "--failed", action="store_true", help="Do failed backups")
group.add_argument("-w", "--weekly", action="store_true", help="Do weekly backups") group.add_argument("-w", "--weekly", action="store_true", help="Do weekly backups")
parser.add_argument(
"-l",
"--list-failed",
action="store_true",
help="List lv's from last failed backup",
)
parser.add_argument( parser.add_argument(
"-o", "--only", help="Only do a backup of the specified lv", metavar="lv_name" "-o", "--only", help="Only do a backup of the specified lv", metavar="lv_name"
) )
...@@ -45,18 +54,47 @@ parser.add_argument( ...@@ -45,18 +54,47 @@ parser.add_argument(
"-t", "-t",
"--test", "--test",
action="store_true", action="store_true",
help="Don't do anything, only show what would have been run.", help="Don't do anything, only show what would have been run",
) )
args = parser.parse_args() args = parser.parse_args()
if not args.test:
if not os.path.exists("/var/run/backstep-traceback-auto"):
os.mkdir("/var/run/backstep-traceback-auto")
JSON_FILE = "/var/run/backstep-traceback-auto/backstep-traceback-auto.json"
else:
JSON_FILE = "backstep-traceback-auto.json"
if os.path.exists(JSON_FILE):
with open(JSON_FILE, encoding="utf8") as json_file:
json_dict = json.load(json_file)
PERIOD = json_dict["period"]
lv_list = json_dict["lv_list"]
if args.daily: if args.daily:
PERIOD = "daily" PERIOD = "daily"
elif args.weekly:
if args.failed:
if os.path.exists(JSON_FILE):
os.remove(JSON_FILE)
if args.weekly:
PERIOD = "weekly" PERIOD = "weekly"
else:
if not PERIOD:
print("error: You need to specify a period [daily or weekly].") print("error: You need to specify a period [daily or weekly].")
sys.exit(2) sys.exit(2)
if args.list_failed:
if lv_list:
print(json.dumps(lv_list, indent=4))
else:
print("error: There is no failed backup list.")
sys.exit(0)
if not args.test: if not args.test:
CONFIG_FILE = "/etc/backstep-traceback/backstep-traceback-auto.ini" CONFIG_FILE = "/etc/backstep-traceback/backstep-traceback-auto.ini"
else: else:
...@@ -110,9 +148,12 @@ else: ...@@ -110,9 +148,12 @@ else:
if bwlimit > 0: if bwlimit > 0:
syncopts.append("--bwlimit=" + str(bwlimit)) syncopts.append("--bwlimit=" + str(bwlimit))
stack = [] if not lv_list:
lv_list = ast.literal_eval(config.get(PERIOD, "backup_lvs"))
stack_dict = {}
for lv in reversed(ast.literal_eval(config.get(PERIOD, "backup_lvs"))): for lv in lv_list:
if args.only and args.only != lv: if args.only and args.only != lv:
continue continue
...@@ -166,7 +207,7 @@ for lv in reversed(ast.literal_eval(config.get(PERIOD, "backup_lvs"))): ...@@ -166,7 +207,7 @@ for lv in reversed(ast.literal_eval(config.get(PERIOD, "backup_lvs"))):
) )
) )
stack.append(command_line) stack_dict[lv] = command_line
# Rsync Exit Values # Rsync Exit Values
# 0 Success # 0 Success
...@@ -190,20 +231,36 @@ for lv in reversed(ast.literal_eval(config.get(PERIOD, "backup_lvs"))): ...@@ -190,20 +231,36 @@ for lv in reversed(ast.literal_eval(config.get(PERIOD, "backup_lvs"))):
# 30 Timeout in data send/receive # 30 Timeout in data send/receive
# 35 Timeout waiting for daemon connection # 35 Timeout waiting for daemon connection
exit_list = [0, 1, 2, 3, 4, 6, 13, 14, 20, 21, 22, 24, 25] exit_list = [1, 2, 3, 4, 6, 13, 14, 20, 21, 22, 24, 25]
wait_list = [5, 10, 11, 12, 23, 30, 35, 255] wait_list = [5, 10, 11, 12, 23, 30, 35, 255]
while stack: lv_list.reverse()
task = stack.pop()
EXIT_CODE = subprocess.call(task) while lv_list:
lv = lv_list.pop()
if EXIT_CODE in exit_list: EXIT_CODE = subprocess.call(stack_dict[lv])
sys.exit(EXIT_CODE)
elif EXIT_CODE in wait_list: if EXIT_CODE in wait_list:
stack.append(task) lv_list.append(lv)
if args.verbose: if args.verbose:
print("Sleeping...") print("Sleeping...")
time.sleep(3030) time.sleep(3030)
elif EXIT_CODE in exit_list:
lv_list.append(lv)
lv_list.reverse()
json_dict = {}
json_dict["datetime"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
json_dict["exitcode"] = EXIT_CODE
json_dict["period"] = PERIOD
json_dict["lv_list"] = lv_list
with open(JSON_FILE, "w", encoding="utf8") as json_file:
json.dump(json_dict, json_file, indent=4)
sys.exit(EXIT_CODE)
sys.exit(0)
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