diff --git a/modules/base/templates/backups/wikitide-backup.py.erb b/modules/base/templates/backups/wikitide-backup.py.erb index 8be26a3aa0..114f54f756 100644 --- a/modules/base/templates/backups/wikitide-backup.py.erb +++ b/modules/base/templates/backups/wikitide-backup.py.erb @@ -19,10 +19,12 @@ from datetime import datetime token = None parser = argparse.ArgumentParser(description='Provides backup and download functionality for WikiTide backups from PCA.') -parser.add_argument('action', choices=['backup', 'download', 'find', 'unfreeze'], help='Action to be ran') +parser.add_argument('action', choices=['backup', 'download', 'find', 'delete', 'unfreeze'], help='Action to be ran') parser.add_argument('type', choices=['private', 'sslkeys', 'phorge', 'sql', 'mediawiki-xml', 'swift-account-container', 'grafana'], help='Type of backup to handle using the action') parser.add_argument('--date', dest='date', help='Date for backup to download', metavar='YYYY-MM-DD') parser.add_argument('--database', dest='database', help='Specific database to download or backup') +parser.add_argument('--delete-all', dest='delete_all', required=False, action='store_true', default=False, help='Delete all backups for the "type" selected') +parser.add_argument('--file-name', dest='file_name', help='Specify the file to delete. To be used with "delete" action') args = parser.parse_args() @@ -59,6 +61,8 @@ def pca_web(method: str, url: str, expiry: int): return requests.post(url, headers=headers, proxies=proxies) elif method == 'HEAD': return requests.head(url, headers=headers, proxies=proxies) + elif method == 'DELETE': + return requests.delete(url, headers=headers, proxies=proxies) else: raise Exception('Method is not currently implemented in WikiTide-Backup') @@ -281,6 +285,24 @@ def find_backups(source: str, database: str): print(backup_item) +def delete_backups(source: str, database: str, delete_all: bool, file_name: str): + if delete_all: + if file_name: + parser.exit(1, 'Cannot use --delete-all with --file-name.') + + all_backups = pca_web('GET', f'https://storage.bhs.cloud.ovh.net/v1/AUTH_76f9bc606a8044e08db7ebd118f6b19a/{source}', 0) + backups_list = list(all_backups.text.split("\n")) + for backup_item in backups_list: + pca_web('DELETE', f'https://storage.bhs.cloud.ovh.net/v1/AUTH_76f9bc606a8044e08db7ebd118f6b19a/{source}/{backup_item}', 0) + print(f'Deleted file {backup_item}') + else: + if not file_name: + parser.exit(1, 'Must specify --file-name with the file you want deleting.') + + pca_web('DELETE', f'https://storage.bhs.cloud.ovh.net/v1/AUTH_76f9bc606a8044e08db7ebd118f6b19a/{source}/{file_name}', 0) + print(f'Deleted file {file_name}') + + def unfreeze_backup(source: str, dt: str, database: str): if source in ['private', 'sslkeys', 'phorge', 'swift-account-container']: file = f'{source}/{dt}.tar.gz' @@ -306,6 +328,8 @@ if __name__ == '__main__': download(args.type, args.date, args.database) elif args.action == 'find': find_backups(args.type, args.database) + elif args.action == 'delete': + delete_backups(args.type, args.database, args.delete_all, args.file_name) elif args.action == 'unfreeze': if not args.date: parser.exit(1, '--date is required when unfreezing a file!')