Skip to content

Instantly share code, notes, and snippets.

@Chuckame
Last active October 24, 2024 10:17
Show Gist options
  • Save Chuckame/e5c63d8eb8ccd94f7bae585feead346b to your computer and use it in GitHub Desktop.
Save Chuckame/e5c63d8eb8ccd94f7bae585feead346b to your computer and use it in GitHub Desktop.
Backup and restore immich database: scripts made to easily backup the immich database in a docker compose env. You need the `DB_BACKUP` and `DB_BACKUPS_FOLDER` set in your `.env`, then just execute `./db-backup.sh`
#!/usr/bin/env bash
####
# This script use .env where should be defined DB_FOLDER (the PG data folder) and DB_BACKUPS_FOLDER (the backups location).
# It stops the immich stack then make the backup by zipping the db data to a tar file.
####
set -a
source .env
set +a
set -e
if [ -z "${DB_FOLDER}" ]; then
echo "DB_FOLDER env var is missing from the .env file"
exit 1
fi
if [ ! -d "${DB_FOLDER}" ]; then
echo "$DB_FOLDER is not a folder or doesn't exist"
exit 1
fi
if [ -z "${DB_BACKUPS_FOLDER}" ]; then
echo "DB_BACKUPS_FOLDER env var is missing from the .env file"
exit 1
fi
if [ ! -d "${DB_BACKUPS_FOLDER}" ]; then
echo "$DB_BACKUPS_FOLDER is not a folder or doesn't exist"
exit 1
fi
echo "DB folder: $DB_FOLDER"
echo "DB backups folder: $DB_BACKUPS_FOLDER"
mkdir -p "$DB_BACKUPS_FOLDER"
docker compose stop
cd "$DB_FOLDER"
tar cf - . -P | pv -s $(du -sb "$DB_FOLDER" | awk '{print $1}') | gzip > "$DB_BACKUPS_FOLDER/$(date --iso-8601=seconds).tar.gz"
#!/usr/bin/env bash
####
# This script use .env where should be defined DB_FOLDER (the PG data folder) and DB_BACKUPS_FOLDER (the backups location).
# It:
# - ask which backup to use (exit if no backup existing)
# - stops the immich stack
# - makes the backup by zipping the db data to a tar file (never too much backups).
# - removes the db data folder content
# - copy back the backup content to the db data folder
# - starts the immich stack now running on the selected backup
####
set -a
source .env
set +a
set -e
if [ -z "${DB_FOLDER}" ]; then
echo "DB_FOLDER env var is missing from the .env file"
exit 1
fi
if [ ! -d "${DB_FOLDER}" ]; then
echo "$DB_FOLDER is not a folder or doesn't exist"
exit 1
fi
if [ -z "${DB_BACKUPS_FOLDER}" ]; then
echo "DB_BACKUPS_FOLDER env var is missing from the .env file"
exit 1
fi
if [ ! -d "${DB_BACKUPS_FOLDER}" ]; then
echo "$DB_BACKUPS_FOLDER is not a folder or doesn't exist"
exit 1
fi
echo "DB folder: $DB_FOLDER"
echo "DB backups folder: $DB_BACKUPS_FOLDER"
unset SELECTED_BACKUP
select f in $(ls $DB_BACKUPS_FOLDER); do
if [ ! -z $f ]; then
echo "You selected $f"
SELECTED_BACKUP=$f
break
fi
echo "Bad input"
done
if [ -z $SELECTED_BACKUP ]; then
echo "No backup selected to restore"
exit 1
fi
docker compose stop
echo "Doing backup..."
cd "$DB_FOLDER"
tar cf - . -P | pv -s $(du -sb "$DB_FOLDER" | awk '{print $1}') | gzip > "$DB_BACKUPS_FOLDER/$(date --iso-8601=seconds).tar.gz"
cd ..
echo "Removing DB..."
rm -rf "$DB_FOLDER"/*
echo "Restoring backup $SELECTED_BACKUP..."
pv "$DB_BACKUPS_FOLDER/$SELECTED_BACKUP" | tar xzpC "$DB_FOLDER/"
docker compose start
@Chuckame
Copy link
Author

All the checks are now done, and uses now docker compose without the dash 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment