Skip to content

Instantly share code, notes, and snippets.

@Chuckame
Last active September 14, 2024 09:03
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
@mmomjian
Copy link

mmomjian commented Jul 30, 2024

I strongly disagree with running rm -rf $DB_FOLDER/* without even checking for that variable to be set… if it’s empty you just nuked your entire Linux system.

You’re missing double quotes around all your variables, so spaces won’t work.

Immich does not support docker-compose — only docker compose.

I would advise caution before anyone uses this. Especially as a backup solution, these could be major issues.

@Chuckame
Copy link
Author

Chuckame commented Jul 30, 2024

I strongly disagree with running rm -rf $DB_FOLDER/* without even checking for that variable to be set… if it’s empty you just nuked your entire Linux system.

At the beginning, if db-folder is empty, then you are not able to go for forward so you cannot rm rf the world. But good point, I'll fix it explicitly.

You’re missing double quotes around all your variables, so spaces won’t work.

You're right, I don't have whitespace in my system as it is a bad practice imo, but I'll fix it

Immich does not support docker-compose — only docker compose.

Your computer/server/os/etc don't support it. On my side it's the reverse, it doesn't support docker compose. Don't make assumption without asking or trying the script.

I would advise caution before anyone uses this. Especially as a backup solution, these could be major issues.

As this script takes care of stopping the database first, the only risk is if you restart the db container with another pg version.

@mmomjian
Copy link

I don’t see where you check for an empty DB_FOLDER?

docker-compose is not supported. This is not about my system … https://immich.app/docs/install/requirements

@Chuckame
Copy link
Author

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 ?

@ringuerel
Copy link

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.

@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