-
Notifications
You must be signed in to change notification settings - Fork 0
Create Back Ups for NS8 Apps
Kemboi Elvis edited this page Jul 25, 2024
·
2 revisions
Important files needed for backup inside the etc folder:
-
state-include.conf
This is an important file needed for backup. Here we store all our states and volumes needed to be restored later.Example of
state-include.conf:
state/smarthost.env
state/kickstart.env
state/database.env
state/zitadel.pg_dump
volumes/kickstart-app
This folder contains the backup scripts especially for databases which are the most important to back up.
- This is an example of a module clean state backup script
rm -vf kickstart.pg_dump- This is an example of a db dumbing script
set -e
echo "Dumping kickstart postgres database"
podman exec postgresql-app pg_dump -U postgres --format=c kickstart > kickstart.pg_dump
As from the name, Scripts inside this folder are used to restore the module to its previous state.
Inside here there are about 4 files which are important.
Important Note
The way the files are arranged it how the files are executed in the restoration process.
-
copyenv.py- This script is used to copy the environment variables from the backup to the current module.
example
import sys
import json
import agent
request = json.load(sys.stdin)
original_environment = request['environment']
for evar in [
"TRAEFIK_HOST",
"TRAEFIK_HTTP2HTTPS",
"TRAEFIK_LETS_ENCRYPT",
]:
agent.set_env(evar, original_environment[evar])
agent.dump_env()-
restore-postgres.sh- This script is used to restore the postgres database from the backup.
example
#!/bin/bash
set -e -o pipefail
exec 1>&2 # Redirect any output to the journal (stderr)
mkdir -vp restore
cat - >restore/kickstart_restore.sh <<'EOS'
# Read dump file from standard input:
pg_restore --no-owner --no-privileges -U postgres -d kickstart
ec=$?
docker_temp_server_stop
exit $ec
EOS
# Override the image /docker-entrypoint-initdb.d contents, to restore the
# DB dump file. The container will be stopped at the end
podman run \
--rm \
--interactive \
--network=none \
--volume=./restore:/docker-entrypoint-initdb.d/:Z \
--volume=postgres-data:/var/lib/postgresql/data:Z \
--replace --name=restore_db \
--env-file=database.env \
--env TZ=UTC \
"${POSTGRES_IMAGE}" < kickstart.pg_dump
# If the restore is successful, clean up:
rm -rfv restore/ kickstart.pg_dump-
traefik.py- This script is used to restore the traefik configuration from the backup.
example
#!/usr/bin/env python3
import sys
import json
import agent
import os
request = json.load(sys.stdin)
renv = request['environment']
configure_retval = agent.tasks.run(agent_id=os.environ['AGENT_ID'], action='configure-module', data={
"lets_encrypt": renv["TRAEFIK_LETS_ENCRYPT"] == "True",
"host": renv["TRAEFIK_HOST"],
"http2https": renv["TRAEFIK_HTTP2HTTPS"] == "True",
})
agent.assert_exp(configure_retval['exit_code'] == 0, "The configure-module subtask failed!")-
start-services.sh- This script is used to start the services after restoring the module.
example
#!/bin/bash
set -e
# Redirect any output to the journal (stderr)
exec 1>&2
# If the control reaches this step, the service can be enabled and started
touch smarthost.env
systemctl --user enable --now kickstart.service