From c7ad95cd1fdd43d3bb2fd0effc68a4c23f8af4c4 Mon Sep 17 00:00:00 2001 From: Tibo De Peuter Date: Sun, 20 Oct 2024 14:58:35 +0200 Subject: [PATCH] chore: Rename scripts to k3s scripts --- scripts/gitea/backup-database-k3s.sh | 80 ++++++++++++++++++++++ scripts/nextcloud/backup-database-k3s.sh | 79 +++++++++++++++++++++ scripts/vaultwarden/backup-database-k3s.sh | 77 +++++++++++++++++++++ 3 files changed, 236 insertions(+) create mode 100755 scripts/gitea/backup-database-k3s.sh create mode 100755 scripts/nextcloud/backup-database-k3s.sh create mode 100755 scripts/vaultwarden/backup-database-k3s.sh diff --git a/scripts/gitea/backup-database-k3s.sh b/scripts/gitea/backup-database-k3s.sh new file mode 100755 index 0000000..08cd4c4 --- /dev/null +++ b/scripts/gitea/backup-database-k3s.sh @@ -0,0 +1,80 @@ +#!/bin/bash +# Backup Gitea database in a Kubernetes environment +# Usage: backup-database [OPTIONS] + +# `gitea dump` is a mess that we should not touch. We write our own backup scripts instead. +# +usage() { + >&2 printf "Usage: %s \n" "${0}" + >&2 printf "Options:\n" + >&2 printf "\t-e \t Specify the environment file to use\n" + exit "${1:-1}" +} + +# Get options + +while getopts ":e:" option; do + case "${option}" in + e) + if ! [ -f "${OPTARG}" ]; then + >&2 printf "Error: Specified environment file does not exist: '%s'.\n" "${OPTARG}" + elif ! [ -r "${OPTARG}" ]; then + >&2 printf "Error: Specified environment file is not readable: '%s'.\n" "${OPTARG}" + fi + env_file="${OPTARG}" + ;; + *) + >&2 printf "Error: Invalid option: '%s'.\n" "${option}" + usage + ;; + esac +done +shift $(( OPTIND - 1 )) + +# Check arguments. + +if [ $# -ne 1 ]; then + >&2 printf "Error: You need to specify a destination.\n" + usage +elif ! [ -d "${1}" ]; then + >&2 printf "Error: Specified destination does not exist or is not readable : '%s'.\n" "${1}" + usage +else + destination="${1}" +fi + +# Retrieve container names +base_container="$( docker ps --format '{{.Names}}' | grep -E 'gitea_gitea-[0-9a-z]{10}-[0-9a-z]{5}' )" +database_container="$( docker ps --format '{{.Names}}' | grep 'postgres_gitea-cnpg-main-1' )" + +if ! [[ -n "${base_container}" && -n "${database_container}" ]]; then + >&2 printf "Error: Not all containers could be found.\n" + exit 2 +fi + +# Abort entire script if any command fails +set -e + +# Enable maintenance mode +# Flush queues +docker exec "${base_container}" gitea manager flush-queues +# TODO Pause queues + +# Database backup + +# Filename for database backup. +database_backupfile="gitea-sqlbkp_$( date +'%Y%m%d' ).bak" +host_database_backupfile="${destination}/${database_backupfile}" + +# Backup the database +>&2 echo 'Backing up database' +docker exec --env-file "${env_file:=.env}" "${database_container}" pg_dump 'gitea' -cwv -h 'localhost' -U 'gitea' > "${host_database_backupfile}" + +# Disable maintenance mode +# TODO Continue queues + +# Double check +# gitea doctor --all --log-file /tmp/doctor.log +# TODO + +printf "Done.\n" diff --git a/scripts/nextcloud/backup-database-k3s.sh b/scripts/nextcloud/backup-database-k3s.sh new file mode 100755 index 0000000..6105468 --- /dev/null +++ b/scripts/nextcloud/backup-database-k3s.sh @@ -0,0 +1,79 @@ +#!/bin/bash +# Backup Nextcloud database in a Kubernetes environment. +# Usage: backup-database + +usage() { + >&2 printf "Usage: %s \n" "${0}" + exit "${1:-1}" +} + +# Get options + +while getopts ":e:" option; do + case "${option}" in + e) + if ! [ -f "${OPTARG}" ]; then + >&2 printf "Error: Specified environment file does not exist: '%s'.\n" "${OPTARG}" + elif ! [ -r "${OPTARG}" ]; then + >&2 printf "Error: Specified environment file is not readable: '%s'.\n" "${OPTARG}" + fi + env_file="${OPTARG}" + ;; + *) + >&2 printf "Error: Invalid option: '%s'.\n" "${option}" + usage + ;; + esac +done +shift $(( OPTIND - 1 )) + +# Check arguments. + +if [ $# -ne 1 ]; then + >&2 printf "Error: You need to specify a destination.\n" + usage +elif ! [ -d "${1}" ]; then + >&2 printf "Error: Specified destination does not exist or is not readable : '%s'.\n" "${1}" + usage +else + destination="${1}" +fi + +# Filename for database backup +database_backupfile="nextcloud-sqlbkp_$(date +'%Y%m%d').bak" + +# Retrieve container names +base_container="$( docker ps --format '{{.Names}}' | grep -E 'nextcloud-2_nextcloud-2-[0-9a-z]{10}-[0-9a-z]{5}' )" +database_container="$( docker ps --format '{{.Names}}' | grep postgres_nextcloud-2-cnpg-main-1 )" + +if ! [[ -n "${base_container}" && -n "${database_container}" ]]; then + >&2 printf "Error: Not all containers could be found.\n" + exit 2 +fi + +# Abort entire script if any command fails +set -e + +# Turn on maintenance mode +docker exec "${base_container}" php occ maintenance:mode --on + +# Database backup +echo 'Backing up database' +host_database_backupfile="${destination}/${database_backupfile}" +docker exec --env-file "${env_file:=.env}" "${database_container}" pg_dump 'nextcloud' -cwv -h 'localhost' -U 'nextcloud' > "${host_database_backupfile}" + +# Files backup +for file in 'config' 'themes'; do + printf "Copying %s\n" "${file}" + docker cp -a "${base_container}":"/var/www/html/${file}" "${destination}" +done + +# Turn off maintenance mode +docker exec "${base_container}" php occ maintenance:mode --off + +# Backup cleanup +# Only keep 30 days of backups +printf "Clean up old database backups.\n" +find "${destination}" -name '*sqlbkp*' -type f -mtime +30 -print -delete + +printf "Done\n" diff --git a/scripts/vaultwarden/backup-database-k3s.sh b/scripts/vaultwarden/backup-database-k3s.sh new file mode 100755 index 0000000..0b0c640 --- /dev/null +++ b/scripts/vaultwarden/backup-database-k3s.sh @@ -0,0 +1,77 @@ +#!/bin/bash +# Backup Vaultwarden database in a Kubernetes environment. +# Usage: backup-database [OPTIONS] + +usage() { + >&2 printf "Usage: %s \n" "${0}" + >&2 printf "Options:\n" + >&2 printf "\t-e \t Specify the environment file to use\n" + exit "${1:-1}" +} + +# Get options + +while getopts ":e:" option; do + case "${option}" in + e) + if ! [ -f "${OPTARG}" ]; then + >&2 printf "Error: Specified environment file does not exist: '%s'.\n" "${OPTARG}" + elif ! [ -r "${OPTARG}" ]; then + >&2 printf "Error: Specified environment file is not readable: '%s'.\n" "${OPTARG}" + fi + env_file="${OPTARG}" + ;; + *) + >&2 printf "Error: Invalid option: '%s'.\n" "${option}" + usage + ;; + esac +done +shift $(( OPTIND - 1 )) + +# Check arguments + +if [ $# -ne 1 ]; then + >&2 printf "Error: You need to specify a destination.\n" + usage +elif ! [ -d "${1}" ]; then + >&2 printf "Error: Specified destination does not exist or is not readable : '%s'.\n" "${1}" + usage +else + destination="${1}" +fi + +# Retrieve container names +base_container="$( docker ps --format '{{.Names}}' | grep -E 'vaultwarden-2_vaultwarden-2-[0-9a-z]{10}-[0-9a-z]{5}' )" +database_container="$( docker ps --format '{{.Names}}' | grep postgres_vaultwarden-2-cnpg-main-1 )" + +if ! [[ -n "${base_container}" && -n "${database_container}" ]]; then + >&2 printf "Error: Not all containers could be found.\n" + exit 2 +fi + +# Abort entire script if any command fails +set -e + +# Database backup + +# Filename for database backup +database_backupfile="vaultwarden-sqlbkp_$(date +'%Y%m%d').bak" +host_database_backupfile="${destination}/${database_backupfile}" + +# Create backup file in docker container +echo 'Backing up database' +docker exec --env-file "${env_file:=.env}" "${database_container}" pg_dump 'vaultwarden' -cwv -h 'localhost' -U 'vaultwarden' > "${host_database_backupfile}" + +# Files backup +for file in 'attachments' 'sends' 'rsa_key.pem' 'rsa_key.pub.pem'; do # 'config.json' + printf 'Copying %s\n' "${file}" + docker cp -a "${base_container}":"/data/${file}" "${destination}" +done + +# Backup cleanup +# Only keep 30 days of backups, seems about right. +echo 'Cleaning up old database backups' +find "${destination}" -name '*sqlbkp*' -type f -mtime +30 -print -delete + +echo 'Done'