Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 17 additions & 18 deletions packages/ns-api/files/ns.backup
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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):
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions packages/ns-plug/files/remote-backup
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/ns-plug/files/send-backup
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading