Simple InvenTree Docker backup using restic

backup.sh
#!/bin/bash
export NAME=$(basename $(pwd))
export RESTIC_REPOSITORY=rest:http://restic:abc123@10.1.2.3:16383/$NAME
export RESTIC_PASSWORD_FILE=.restic_password

if [ ! -f "${RESTIC_PASSWORD_FILE}" ]; then
   echo "Please create .restic_password with the backup encryption password AND BACKUP THAT PASSWORD SEPARATELY!!!"
   exit 1
fi

echo "Initing repo, please ignore any 'already exists' errors"
if [ ! -f ".restic_inited" ]; then
    # Run the restic init command
    restic init

    if [ $? -eq 0 ]; then # if init successful
        # Create the initialization file
        touch ".restic_inited"
        echo "Restic initialized"
    fi
fi

# Run postgreSQL backup in streaming mode
source .env
docker-compose exec -T inventree-db pg_dump -U${INVENTREE_DB_USER} ${INVENTREE_DB_NAME} | restic --verbose backup --stdin --stdin-filename="inventree-pgdump.sql"


# Backup directories and files
echo "Backing up with restic..."
restic --verbose backup \
    inventree_data \
    backup.sh \
    docker-compose.yml \
    --exclude "**/__pycache__" \
    --exclude "inventree_data/static" \
    --exclude "inventree_data/pgdb" \
    --exclude "inventree_data/backup"

# OPTIONAL: Forget old backups and prune
# Adjust these retention policies as needed:
# --keep-daily 31    # keep 31 daily snapshots
# --keep-weekly 52   # keep 52 weekly snapshots
# --keep-monthly 24  # keep 24 monthly snapshots
# --keep-yearly 10  # keep 10 yearly snapshots
restic forget --keep-hourly 168 --keep-daily 31 --keep-weekly 52 --keep-monthly 24 --keep-yearly 10 --prune

echo "Backup complete!"

Check out similar posts by category: InvenTree