I use PostgreSQL on docker and i want to take back up in everyday.
I tired to enter linux and take back up every night.
And i made a decided. Why i don’t have a cron job? Let’s start;
- Step 1: Check the cron job list
crontab -l
- Step 2: Edit cron file. The # sign is the comment line. If you remove the sharp sign, it is now operational.
crontab -e

- Step 3: Next step is add running command for us.
*/1 * * * * docker exec -t postgres pg_dumpall -c -U postgres | gzip > /home/parallels/Desktop/docker-yml/dump-`date +\%Y\%m\%d\%H\%M\%S`-backup.gz

- Step 4: We must learn to cron time expression. Click for Source!
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
17 * * * * root cd / && run-parts --report /etc/cron.hourly
If we want to check job logs;
grep CRON /var/log/syslog
Bonus: Restore command;
cat your_dump.sql | docker exec -i your-db-container psql -U postgres
Congratulations!