-
-
Save Chuckame/e5c63d8eb8ccd94f7bae585feead346b to your computer and use it in GitHub Desktop.
#!/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 |
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
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 ?
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 🚀
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 right, I don't have whitespace in my system as it is a bad practice imo, but I'll fix it
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.As this script takes care of stopping the database first, the only risk is if you restart the db container with another pg version.