Postgres backup script not working when executed by a cronjob
Postgres backup script not working when executed by a cronjob
I've made the following backup script for my immich stack to be automatically run every day
sh
# Load variables from the .env file SCRIPT_DIR="$(dirname "$(readlink -f "$0")")" set -a source "$SCRIPT_DIR/../.env" set +a # Create a dump of the database and back it up docker exec -it immich_db pg_dumpall -c -U immich > immich/latest_db_dump.sql rustic -r $BUCKET_NAME/immich backup immich/latest_db_dump.sql --password=$REPO_PWD # Backup the library, uploads and profile folders from the upload volume rustic -r $BUCKET_NAME/immich backup immich/uploads_v/library --password=$REPO_PWD rustic -r $BUCKET_NAME/immich backup immich/uploads_v/upload --password=$REPO_PWD rustic -r $BUCKET_NAME/immich backup immich/uploads_v/profile --password=$REPO_PWD # Apply forget policy rustic -r $BUCKET_NAME/immich forget $RUSTIC_FORGET_POLICY --password=$REPO_PWD
and when I test it everything works properly, and the created sql dump file is complete and properly backed up.
However, when the execution is triggered automatically by a cronjob (as specified in this crontab line)
"30 3 * * * root /home/admin/WinguRepo/scripts/docker_backupper.sh"
(the line is taken from the nixos configuration file, that's why it also contains the user executing the operation)
it seems something breaks in the dumping process, because the script completes successfully but the sql dump file is an empty file (as can be noticed in the following output of rustic -r myrepo snapshots
snapshots for (host [wingu-box], label [], paths [immich/latest_db_dump.sql]) | ID | Time | Host | Label | Tags | Paths | Files | Dirs | Size | |----------|---------------------|-----------|-------|------|---------------------------|-------|------|-----------| | 10a32a83 | 2025-01-06 20:56:48 | wingu-box | | | immich/latest_db_dump.sql | 1 | 2 | 264.6 MiB | | 1174bc2e | 2025-01-07 12:50:36 | wingu-box | | | immich/latest_db_dump.sql | 1 | 2 | 264.6 MiB | | 00977334 | 2025-01-08 03:31:24 | wingu-box | | | immich/latest_db_dump.sql | 1 | 2 | 0 B | | 513fffa1 | 2025-01-10 03:31:25 | wingu-box | | | immich/latest_db_dump.sql | 1 | 2 | 0 B | 4 snapshot(s)
(the first two snapshots were manually triggered by me executing the script, the latter two instead are triggered automatically by the cronjob)
Any idea about what is causing this behavior?
EDIT: Solution found thank's to @farcaller@fstab.sh comment:
You don’t need
-it
because you don’t run an interactive session in docker. It might be failing because you ask for a pseudoterminal in an environment where it doesn’t make sense.