chore: add backup script
This commit is contained in:
parent
105f4757f2
commit
983efdbfcc
1 changed files with 116 additions and 0 deletions
116
backup.sh
Executable file
116
backup.sh
Executable file
|
|
@ -0,0 +1,116 @@
|
|||
# stops the script immediately if any error is encountered
|
||||
set -e
|
||||
|
||||
PINK="\033[35m"
|
||||
RESET="\033[0m"
|
||||
|
||||
backup_dir() {
|
||||
# $1 = directory to (relative to backup folder)
|
||||
# $2 = location of files
|
||||
|
||||
echo -e "$PINK $1: backing up directory $2$RESET"
|
||||
OUT="$BACKUP_DIR"/"$1"
|
||||
rsync -avP --no-perms --no-owner --no-group "$2/" "$OUT/"
|
||||
}
|
||||
|
||||
systemd_stop_and_backup_dir() {
|
||||
# $1 = service name
|
||||
# $2 = directory to backup
|
||||
|
||||
echo -e "$PINK $1: stopping service$RESET"
|
||||
systemctl stop "$1"
|
||||
|
||||
backup_dir "$1" "$2"
|
||||
|
||||
echo -e "$PINK $1: starting service$RESET"
|
||||
systemctl start "$1"
|
||||
|
||||
echo -e "$PINK $1: DONE$RESET"
|
||||
}
|
||||
|
||||
systemd_stop_and_backup_dir_arr() {
|
||||
# excludes a couple not-needed large directories
|
||||
# present in arr applications
|
||||
# $1 = service name
|
||||
# $2 = directory to backup
|
||||
|
||||
echo -e "$PINK $1: stopping service$RESET"
|
||||
systemctl stop "$1"
|
||||
|
||||
echo -e "$PINK $1: backing up directory $2$RESET"
|
||||
OUT="$BACKUP_DIR"/"$1"
|
||||
mkdir --parents "$OUT"
|
||||
rsync -avP --no-perms --no-owner --no-group --exclude Backups --exclude logs "$2/" "$OUT/"
|
||||
|
||||
echo -e "$PINK $1: starting service$RESET"
|
||||
systemctl start "$1"
|
||||
|
||||
echo -e "$PINK $1: DONE$RESET"
|
||||
}
|
||||
|
||||
kanidm_backup() {
|
||||
echo -e "$PINK kanidm: backing up database to temp file$RESET"
|
||||
DATABASE_DUMP_PATH=$(mktemp)
|
||||
chown kanidm:kanidm "$DATABASE_DUMP_PATH"
|
||||
run0 -u kanidm --unit=kanidmd sh -c "kanidmd database backup \"$DATABASE_DUMP_PATH\""
|
||||
|
||||
echo -e "$PINK kanidm: copying over to backup location$RESET"
|
||||
OUT="$BACKUP_DIR/kanidm"
|
||||
rsync -avP --no-perms --no-owner --no-group "$DATABASE_DUMP_PATH" "$OUT"
|
||||
rm "$DATABASE_DUMP_PATH"
|
||||
|
||||
echo -e "$PINK kanidm: DONE$RESET"
|
||||
}
|
||||
|
||||
postgres_backup() {
|
||||
echo -e "$PINK postgres($1): backing up database $1 to temp file$RESET"
|
||||
DATABASE_DUMP_PATH=$(mktemp)
|
||||
chown postgres:postgres "$DATABASE_DUMP_PATH"
|
||||
run0 -u postgres pg_dump --dbname="$1" --file="$DATABASE_DUMP_PATH"
|
||||
|
||||
echo -e "$PINK postgres($1): copying over to backup location$RESET"
|
||||
OUT="$BACKUP_DIR/$1"
|
||||
rsync -avP --no-perms --no-owner --no-group "$DATABASE_DUMP_PATH" "$OUT"
|
||||
rm "$DATABASE_DUMP_PATH"
|
||||
|
||||
echo -e "$PINK postgres($1): DONE$RESET"
|
||||
}
|
||||
|
||||
forgejo_backup() {
|
||||
echo -e "$PINK forgejo: backing up$RESET"
|
||||
OUT="$BACKUP_DIR"/"forgejo"
|
||||
|
||||
# gets binary name from the systemd service, this works
|
||||
# with multiple derivations in the nix store and grabs
|
||||
# the binary of the latest package.
|
||||
FORGEJO_PATH=$(cat /etc/systemd/system/forgejo.service | grep ExecStart= | sed 's/ExecStart=//' | awk '{print $1;}')
|
||||
run0 -u forgejo "$FORGEJO_PATH" dump --config /media/git/custom/conf/app.ini -f - > "$OUT"
|
||||
|
||||
echo -e "$PINK forgejo: DONE$RESET"
|
||||
}
|
||||
|
||||
if [ "$1" = "" ]; then
|
||||
echo "$0: [BACKUP_DIR]"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
BACKUP_DIR="$1"
|
||||
echo -e "$PINK using folder $BACKUP_DIR$RESET"
|
||||
mkdir --parents "$BACKUP_DIR"
|
||||
|
||||
systemd_stop_and_backup_dir_arr radarr /var/lib/radarr
|
||||
systemd_stop_and_backup_dir_arr sonarr /var/lib/sonarr
|
||||
systemd_stop_and_backup_dir_arr prowlarr /var/lib/prowlarr
|
||||
systemd_stop_and_backup_dir uptime-kuma /var/lib/uptime-kuma
|
||||
backup_dir deluge /media/torrents/files
|
||||
|
||||
# karakeep has multiple services so stop all of them
|
||||
systemctl stop karakeep-web karakeep-browser karakeep-workers
|
||||
systemd_stop_and_backup_dir karakeep-web /var/lib/karakeep
|
||||
systemctl start karakeep-web karakeep-browser karakeep-workers
|
||||
|
||||
kanidm_backup
|
||||
forgejo_backup
|
||||
postgres_backup miniflux
|
||||
|
||||
echo -e "\n\nBACKUP COMPLETED!"
|
||||
Loading…
Add table
Add a link
Reference in a new issue