Last active
October 24, 2024 10:17
-
-
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`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
as long as it does what it say I'll try to use it, if the comments above provide actual improvement, please focus on the improve opportunities regardless how strong or weak it was pointed.
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
You're right, it's checking DB_BACKUPS_FOLDER. I updated the script to exit when the variables are empty or not folders.
docker-compose with the dash is just the old compose system which is now integrated as a plugin in docker. They are just saying that this could be not compatible. For a standard simple usage of docker compose, docker-compose should still work (and work on my computer). On your side, if it's not working, then just remove the dash
-
on your side ?