updates backup system, cleanup backup list and add backup deletion

This commit is contained in:
kjeld Schouten-Lebbing 2022-01-20 15:45:13 +01:00
parent b540499b4d
commit 1c41a77e12
No known key found for this signature in database
GPG Key ID: 4CDAD4A532BC1EDB
3 changed files with 42 additions and 18 deletions

View File

@ -34,8 +34,9 @@ Additional options are available:
### Backup ### Backup
- `truetool -b` or ` truetool --backup` backup the complete Apps system prior to updates - `truetool -b` or ` truetool --backup` backup the complete Apps system prior to updates. Deletes old backups prior, number of old backups can be set, 14 by default
- `truetool -r` or ` truetool --restore` restores a specific backup by name - `truetool -r` or ` truetool --restore` **WIP** restores a specific backup by name
- `truetool -d` or ` truetool --delete` deletes a specific backup by name
#### Other #### Other

View File

@ -7,7 +7,7 @@ README_MD = open(join(dirname(abspath(__file__)), "README.md")).read()
setup( setup(
name="truetool", name="truetool",
version="2.0.0", version="2.1.0",
# The packages that constitute your project. # The packages that constitute your project.
# For my project, I have only one - "pydash". # For my project, I have only one - "pydash".

View File

@ -97,20 +97,24 @@ def process_args():
global UPDATE global UPDATE
global RESTORE global RESTORE
global LIST global LIST
global DELETE
parser = argparse.ArgumentParser(description='Update TrueNAS SCALE Apps') parser = argparse.ArgumentParser(description='Update TrueNAS SCALE Apps')
parser.add_argument('-c', '--catalog', nargs='?', default='ALL', help='name of the catalog you want to process in caps. Or "ALL" to render all catalogs.') parser.add_argument('-c', '--catalog', nargs='?', default='ALL', help='name of the catalog you want to process in caps. Or "ALL" to render all catalogs.')
parser.add_argument('-v', '--versioning', nargs='?', default='minor', help='Name of the versioning scheme you want to update. Options: major, minor or patch. Defaults to minor') parser.add_argument('-v', '--versioning', nargs='?', default='minor', help='Name of the versioning scheme you want to update. Options: major, minor or patch. Defaults to minor')
parser.add_argument('-b', '--backup', nargs='?', const='14', help='backup the complete Apps system prior to updates, add a number to specify the max old backups to keep')
parser.add_argument('-r', '--restore', nargs='?', help='restore a previous backup, disables all other features')
parser.add_argument('-d', '--delete', nargs='?', help='delete a specific backup')
parser.add_argument('-s', '--sync', action="store_true", help='sync catalogs before trying to update') parser.add_argument('-s', '--sync', action="store_true", help='sync catalogs before trying to update')
parser.add_argument('-u', '--update', action="store_true", help='update the Apps in the selected catalog') parser.add_argument('-u', '--update', action="store_true", help='update the Apps in the selected catalog')
parser.add_argument('-p', '--prune', action="store_true", help='prune old docker images after update') parser.add_argument('-p', '--prune', action="store_true", help='prune old docker images after update')
parser.add_argument('-a', '--all', action="store_true", help='update "active" apps only and ignore "stopped" or "stuck" apps') parser.add_argument('-a', '--all', action="store_true", help='update "active" apps only and ignore "stopped" or "stuck" apps')
parser.add_argument('-b', '--backup', action="store_true", help='backup the complete Apps system prior to updates')
parser.add_argument('-r', '--restore', nargs='?', help='restore a previous backup, disables all other features')
parser.add_argument('-l', '--list', action="store_true", help='lists existing backups') parser.add_argument('-l', '--list', action="store_true", help='lists existing backups')
args = parser.parse_args() args = parser.parse_args()
CATALOG = args.catalog CATALOG = args.catalog
VERSIONING = args.versioning VERSIONING = args.versioning
RESTORE = args.restore RESTORE = args.restore
BACKUP = args.backup
DELETE = args.delete
if args.update: if args.update:
UPDATE = True UPDATE = True
else: else:
@ -127,10 +131,6 @@ def process_args():
ALL = True ALL = True
else: else:
ALL = False ALL = False
if args.backup:
BACKUP = True
else:
BACKUP = False
if args.list: if args.list:
LIST = True LIST = True
else: else:
@ -153,13 +153,20 @@ def sync_catalog():
def docker_prune(): def docker_prune():
if PRUNE: if PRUNE:
print("Pruning old docker images...\n") print("Pruning old docker images...\n")
process = subprocess.Popen(["docker", "image", "prune", "-af"], stdout=subprocess.PIPE) process = subprocess.run(["docker", "image", "prune", "-af"], stdout=subprocess.PIPE)
print("Images pruned.\n") print("Images pruned.\n")
else: else:
print("Container Image Pruning disabled, skipping...") print("Container Image Pruning disabled, skipping...")
def apps_backup(): def apps_backup():
if BACKUP: if BACKUP:
print(f"Cleaning old backups to a max. of {BACKUP}...\n")
backups_fetch = get_backups_names()
backups_cleaned = [k for k in backups_fetch if 'TrueTool' in k]
backups_remove = backups_cleaned[:len(backups_cleaned)-int(BACKUP)]
for backup in backups_remove:
backups_delete(backup)
print("Running App Backup...\n") print("Running App Backup...\n")
now = datetime.now() now = datetime.now()
command = "app kubernetes backup_chart_releases backup_name=TrueTool_"+now.strftime("%Y_%d_%m_%H_%M_%S") command = "app kubernetes backup_chart_releases backup_name=TrueTool_"+now.strftime("%Y_%d_%m_%H_%M_%S")
@ -175,14 +182,28 @@ def apps_backup():
def backups_list(): def backups_list():
if LIST: if LIST:
print("Running App Backup...\n") print("Generating Backup list...\n")
process = subprocess.Popen(["cli", "-c", "app kubernetes list_backups"], stdout=subprocess.PIPE) backups = get_backups_names()
while process.poll() is None: for backup in backups:
lines = process.stdout.readline() print(f"{backup}")
print (lines.decode('utf-8'))
temp = process.stdout.read() def backups_delete(backup: str):
if temp: print(f"removing {backup}...")
print (temp.decode('utf-8')) process = subprocess.run(["midclt", "call", "kubernetes.delete_backup", backup], stdout=subprocess.PIPE)
def get_backups_names():
names = []
process = subprocess.run(["cli", "-c", "app kubernetes list_backups"], stdout=subprocess.PIPE)
output = process.stdout.decode('utf-8')
for line in output.split("\n"):
if line.startswith("+-"):
continue
else:
rowlist = [col.strip() for col in line.strip().split("|") if col and col != ""]
if rowlist:
names.append(rowlist[0])
names.sort()
return names
def apps_restore(): def apps_restore():
print("Running Backup Restore...\n") print("Running Backup Restore...\n")
@ -204,6 +225,8 @@ def run():
apps_restore() apps_restore()
elif LIST: elif LIST:
backups_list() backups_list()
elif DELETE:
backups_delete(DELETE)
else: else:
apps_backup() apps_backup()
sync_catalog() sync_catalog()