Create /root/.pgpass

localhost:5432:DATABASENAME:postgres:PASSWORD

Then:

sudo chmod 0600 /root/.pgpass

Create the script (~/backup.sh):

#!/bin/bash
now="$(date +'%d-%m-%Y')"
printf "Starting files backup, today %s ------------- \n" "$now"
cd /home/USER/backup
zip -r MYFOLDER-$now.zip /home/USER/MYFOLDER
printf "Finished, files backup, today %s ------------- \n" "$now"
printf "Starting database backup, today %s ------------- \n" "$now"
pg_dump DATABASENAME -f DATABASENAME-$now
printf "Finished, database backup, today %s ------------- \n" "$now"

Then:

sudo chmod +x backup.sh

Create the cron job:

sudo crontab -e

Note: If you use just “crontab -e” you have to create your .pgpass inside your home folder: ~/.pgpass, not in the root.
Type in:

0 0 * * * /home/USER/backup.sh

Save. Or, instead, just copy your backup.sh in /etc/cron.daily/