Commit 2567510c authored by Nigel Kukard's avatar Nigel Kukard
Browse files

Merge branch 'failed-backup-options' into 'master'

Add failed and list options

See merge request !48
parents 8962ca0e f0807aa6
Pipeline #6653 passed with stages
in 41 seconds
......@@ -21,6 +21,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import argparse
import ast
import configparser
from datetime import datetime
import json
import os
import subprocess
import sys
......@@ -37,7 +39,14 @@ parser.add_argument(
)
group = parser.add_mutually_exclusive_group()
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")
parser.add_argument(
"-l",
"--list-failed",
action="store_true",
help="List lv's from last failed backup",
)
parser.add_argument(
"-o", "--only", help="Only do a backup of the specified lv", metavar="lv_name"
)
......@@ -45,18 +54,47 @@ parser.add_argument(
"-t",
"--test",
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()
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:
PERIOD = "daily"
elif args.weekly:
if args.failed:
if os.path.exists(JSON_FILE):
os.remove(JSON_FILE)
if args.weekly:
PERIOD = "weekly"
else:
if not PERIOD:
print("error: You need to specify a period [daily or weekly].")
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:
CONFIG_FILE = "/etc/backstep-traceback/backstep-traceback-auto.ini"
else:
......@@ -110,9 +148,12 @@ else:
if bwlimit > 0:
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:
continue
......@@ -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
# 0 Success
......@@ -190,20 +231,36 @@ for lv in reversed(ast.literal_eval(config.get(PERIOD, "backup_lvs"))):
# 30 Timeout in data send/receive
# 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]
while stack:
task = stack.pop()
lv_list.reverse()
EXIT_CODE = subprocess.call(task)
while lv_list:
lv = lv_list.pop()
if EXIT_CODE in exit_list:
sys.exit(EXIT_CODE)
elif EXIT_CODE in wait_list:
stack.append(task)
EXIT_CODE = subprocess.call(stack_dict[lv])
if EXIT_CODE in wait_list:
lv_list.append(lv)
if args.verbose:
print("Sleeping...")
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