How to backup postgresql

#!/bin/bash

# env variables
export AWS_ACCESS_KEY_ID=***
export AWS_SECRET_ACCESS_KEY=***
export AWS_DEFAULT_REGION=***
export AWS_S3_URL=s3://bucket_name

echo $(date +"%Y-%m-%d %T"): START chatfuel_production database backup


# get a dump of database
echo $(date +"%Y-%m-%d %T"): Dump the database
pg_dump -d=$DATABASE_URL | gzip > /var/backups/chatfuel_production-$(date +%Y%m%d).psql.gz


# remove all but 5 latest backups
echo $(date +"%Y-%m-%d %T"): Remove old backups (keep latest 5 backup files)
ls -1trd /var/backups/* | head -n -5 | xargs rm -f


# synchronize folder to s3 backup bucket
echo $(date +"%Y-%m-%d %T"): Sync to Amazon S3
aws s3 sync /var/backups/ $AWS_S3_URL --exclude "*" --include "*.sql"


echo $(date +"%Y-%m-%d %T"): END backup