kartoza / docker-pg-backup

A cron job that will back up databases running in a docker postgres container
GNU General Public License v2.0
452 stars 103 forks source link

Add logging #97

Open YuryHrytsuk opened 11 months ago

YuryHrytsuk commented 11 months ago

Feature description

I want to see in the containers logs when it starts creating a backup, when if finishes, what was created and, if failed, some logs to understand the context of failure. The date time of the logs will be also helpful.

Additional context

Whenever for some reason you don't find a backup file you have no idea what happened and the containers logs stay always empty.

Start script running with these environment options

DUMPPREFIX=PG_backup

PG_CONN_PARAMETERS='-h production_postgres -p 5432 -U postgres'
NyakudyaA commented 11 months ago

You should be able to check the cron logs https://github.com/kartoza/docker-pg-backup/blob/master/build_data/backups-cron#L2C47-L2C63

YuryHrytsuk commented 11 months ago

You should be able to check the cron logs https://github.com/kartoza/docker-pg-backup/blob/master/build_data/backups-cron#L2C47-L2C63

Is it possible show these logs in container's logs?

NyakudyaA commented 11 months ago

https://stackoverflow.com/questions/45395390/see-cron-output-via-docker-logs-without-using-an-extra-file, I suggest you do a PR with the suggested changes here

NyakudyaA commented 11 months ago

Or just add your own custom cron file with that logic

-v ./backups-cron:/settings/backups-cron
YuryHrytsuk commented 10 months ago

Or just add your own custom cron file with that logic

-v ./backups-cron:/settings/backups-cron

It looks like this file will never be used because of these lines:

https://github.com/kartoza/docker-pg-backup/blob/b55f20c57c52834298facfc70b427c9601ed8d3d/scripts/start.sh#L131-L134

YuryHrytsuk commented 10 months ago

Or just add your own custom cron file with that logic

-v ./backups-cron:/settings/backups-cron

It looks like this file will never be used because of these lines:

https://github.com/kartoza/docker-pg-backup/blob/b55f20c57c52834298facfc70b427c9601ed8d3d/scripts/start.sh#L131-L134

I found a workaround: -v ./custom-backups-cron:/build_data/backups-cron.

However, it will still NOT work because inside scripts logs are directly written to the /var/log/cron.log. For example, https://github.com/kartoza/docker-pg-backup/blob/b55f20c57c52834298facfc70b427c9601ed8d3d/scripts/backups.sh#L94

YuryHrytsuk commented 10 months ago

I finally ended up with this:

    volumes:
      - ./custom-backups-cron:/build_data/backups-cron  # redirect logs to /dev/stdout
      - ./custom-backups.sh:/backup-scripts/backups.sh 

First volume is to redirect cron job output to /dev/stdout, i.e.

${CRON_SCHEDULE} /backup-scripts/backups.sh > /proc/1/fd/1 2>&1

Second volume simply removes all >> /var/log/cron.log entries to avoid writing to file.

I will see how it flies. If it works fine, I will create PR @NyakudyaA

NyakudyaA commented 10 months ago

Or just add your own custom cron file with that logic

-v ./backups-cron:/settings/backups-cron

It looks like this file will never be used because of these lines:

https://github.com/kartoza/docker-pg-backup/blob/b55f20c57c52834298facfc70b427c9601ed8d3d/scripts/start.sh#L131-L134

Wouldn't it make sense to adjust the function cron_config(), I think the logic should be If cron file exists in extra_config dir then use it else set default from internal

NyakudyaA commented 10 months ago

Another easier option is to add

If [ Logging== True] ;then
Logging=/dev/stdout
else
Logging=
Fi