diff --git a/packages/ns-api/files/ns.backup b/packages/ns-api/files/ns.backup index 65f8f8fb7..edadeb97e 100755 --- a/packages/ns-api/files/ns.backup +++ b/packages/ns-api/files/ns.backup @@ -5,14 +5,12 @@ # SPDX-License-Identifier: GPL-2.0-only # -import base64 import json import os import shutil import subprocess import sys -import random -import string +from datetime import datetime from nethsec import utils @@ -21,7 +19,7 @@ DOWNLOAD_PATH = '/var/run/ns-api-server/downloads/' PASSPHRASE_PATH = '/etc/backup.pass' def generate_random_file_name(): - return 'backup-' + ''.join(random.choice(string.ascii_letters) for i in range(20)) + return 'backup-' + datetime.now().strftime('%s') + '.tar.gz' def create_backup(encrypted=True): @@ -170,21 +168,22 @@ elif cmd == 'call': print(json.dumps(utils.generic_error(f'remote list failed'))) elif action == 'registered-backup': - if os.path.exists(PASSPHRASE_PATH): + if not os.path.exists(PASSPHRASE_PATH): print(utils.validation_error('passphrase', 'missing')) - try: - # create backup - file_name = create_backup() - backup_path = f'{DOWNLOAD_PATH}{file_name}' - # upload backup to server and remove it from filesystem - completed_process = subprocess.run(['/usr/sbin/remote-backup', 'upload', backup_path], check=True, - capture_output=True) - os.remove(backup_path) - print(json.dumps({'message': 'success'})) - except subprocess.CalledProcessError as error: - print(json.dumps(utils.generic_error(f'remote upload failed'))) - except RuntimeError as error: - print(json.dumps(utils.generic_error(error.args[0]))) + else: + try: + # create backup + file_name = create_backup() + backup_path = f'{DOWNLOAD_PATH}{file_name}' + # upload backup to server and remove it from filesystem + completed_process = subprocess.run(['/usr/sbin/remote-backup', 'upload', backup_path], check=True, + capture_output=True) + os.remove(backup_path) + print(json.dumps({'message': 'success'})) + except subprocess.CalledProcessError as error: + print(json.dumps(utils.generic_error(f'remote upload failed'))) + except RuntimeError as error: + print(json.dumps(utils.generic_error(error.args[0]))) elif action == 'registered-restore': try: diff --git a/packages/ns-plug/files/remote-backup b/packages/ns-plug/files/remote-backup index 123f882ac..9a12d0ef4 100755 --- a/packages/ns-plug/files/remote-backup +++ b/packages/ns-plug/files/remote-backup @@ -59,11 +59,11 @@ case "$cmd" in # proxy, same pattern used by send-heartbeat / send-inventory. # To be removed once the migration is complete. if [ "$TYPE" = "enterprise" ]; then - ext=".$(basename $file | cut -d '.' -f2)" - date="-$(date +%Y-%m-%d)" + # Strip the directory path first to get just the filename + filename="${file##*/}" curl $curl_args -X POST \ -H "Content-Type: application/octet-stream" \ - -H "X-Filename: backup$date$ext" \ + -H "X-Filename: ${filename}" \ --data-binary "@$file" https://my.nethesis.it/proxy/backup >/dev/null || : fi exit $rc diff --git a/packages/ns-plug/files/send-backup b/packages/ns-plug/files/send-backup index fe92dabd0..0cf33f2f0 100755 --- a/packages/ns-plug/files/send-backup +++ b/packages/ns-plug/files/send-backup @@ -11,7 +11,7 @@ set -e WORK_DIR="/var/backup" -BACKUP="$WORK_DIR/backup.tar.gz" +BACKUP="$WORK_DIR/backup-$(date +%s).tar.gz" MD5="$WORK_DIR/md5" MD5_LAST="/etc/backup.md5" PASSPHRASE="/etc/backup.pass"