Skip to content

Commit

Permalink
Enable send-backup timer
Browse files Browse the repository at this point in the history
When a subscription is activated, send immediately a backup if available
and start the timer for the nightly upload.
  • Loading branch information
DavidePrincipi committed Jan 15, 2024
1 parent ee3a18d commit 166b6c0
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 3 deletions.
8 changes: 8 additions & 0 deletions core/imageroot/etc/systemd/system/send-backup.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[Unit]
Description=Subscription cluster cloud backup

[Service]
Type=simple
ExecStart=runagent cluster-backup
ExecStart=runagent send-cluster-backup
SyslogIdentifier=%N
11 changes: 11 additions & 0 deletions core/imageroot/etc/systemd/system/send-backup.timer
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[Unit]
Description=Trigger sending cluster backup for subscription

[Timer]
OnBootSec=600
OnCalendar=3:00:00
Persistent=true
RandomizedDelaySec=21600

[Install]
WantedBy=timers.target
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def terminate_nsent(rdb, attributes):
except:
ofreekey = {} # Ignore remote request failures and disable client services, always.

agent.run_helper("systemctl", "disable", "--now", "send-heartbeat.service", "send-inventory.timer")
agent.run_helper("systemctl", "disable", "--now", "send-heartbeat.service", "send-inventory.timer", "send-backup.timer")

return ofreekey

Expand Down Expand Up @@ -104,8 +104,8 @@ def subscribe_nsent(rdb, osubscription):
trx.publish('cluster/event/subscription-changed', json.dumps({"action": "subscribed"}))
trx.execute()

agent.run_helper("send-inventory")
agent.run_helper("systemctl", "enable", "--now", "send-inventory.timer", "send-heartbeat.service")
agent.run_helper("send-cluster-backup")
agent.run_helper("systemctl", "enable", "--now", "send-inventory.timer", "send-heartbeat.service", "send-backup.timer")

return {
"system_id": system_id,
Expand Down
60 changes: 60 additions & 0 deletions core/imageroot/var/lib/nethserver/cluster/bin/send-cluster-backup
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/bin/bash

#
# Copyright (C) 2024 Nethesis S.r.l.
# SPDX-License-Identifier: GPL-3.0-or-later
#

set -e

# Parse subscription configuration from Redis
while IFS='=' read -r key value; do
declare "${key}"="${value}"
done < <(redis-hgetall cluster/subscription)

function exit_error ()
{
echo "[ERROR]" "${@}" 1>&2
exit 1
}

function send_cluster_backup_nsent ()
{
curl -X POST \
--silent \
--user-agent NethBackup \
--location-trusted \
--user "${system_id:?}:${auth_token:?}" \
--header 'NethBackup-Action: backup-send' \
--header "NethBackup-Parameters: LK=${system_id:?}&fname=dump.json.gz.gpg" \
--data-binary "@${encrypted_file}" \
https://backupd.nethesis.it/backup >/dev/null
}

sendfunc="send_cluster_backup_${provider:?missing subscription config}"
if [[ $(type -t "${sendfunc}") != function ]]; then
echo "[WARNING] Invalid provider, ignored: ${provider}" 1>&2
exit 0
fi

if [[ -f backup/dump.md5 ]]; then
prev_backup_hash="$(< backup/dump.md5)"
else
prev_backup_hash="never_uploaded"
fi

backup_file=backup/dump.json.gz
encrypted_file=backup/dump.json.gz.gpg
backup_hash=$(md5sum "${backup_file}" | awk '{ print $1 }')

if [[ "${prev_backup_hash}" == "${backup_hash}" ]]; then
echo "Backup file already uploaded, nothing to do" 1>&2
elif [[ -f "${encrypted_file}" ]]; then
${sendfunc}
# Backup upload successful, update the reference file
echo "${backup_hash}" > backup/dump.md5
echo "Backup uploaded to provider ${provider} with hash ${backup_hash}" 1>&2
else
echo "[ERROR] Could not find ${encrypted_file} for upload" 1>&2
exit 1
fi

0 comments on commit 166b6c0

Please sign in to comment.