#!/bin/sh

######################################################
# MAKE SURE TO CHANGE VALUES ACCORDING TO YOUR NEEDS #
######################################################

# Create a timestamp for dump names
DAY=$(date +%Y-%m-%d)

# Pick a database server, postgres or mysql
DATABASE=postgres

# Dump file location
DUMPFILE=/root/db_dumps/$DATABASE-backup-$DAY

##########################
# MYSQL-SPECIFIC OPTIONS #
##########################
MYSQLPASSWORD='CHANGEMETOSOMETHINGSECURE'

#############################
# POSTGRES-SPECIFIC OPTIONS #
#############################
# Set the read-only user's password
export PGPASSWORD='CHANGEMETOSOMETHINGSECURETOO'
# Space-separated list of databases for backing up
DBNAME="dbname1 dbname2"

case $DATABASE in
    mysql)
        touch $DUMPFILE
        chown 0:0 $DUMPFILE
        chmod 600 $DUMPFILE
        mysqldump -u archive -p$MYSQLPASSWORD --all-databases > $DUMPFILE
        ;;
    postgres)
        for i in $DBNAME
        do
            PGDUMP=$DUMPFILE-$i
            touch $PGDUMP
            chown 0:0 $PGDUMP
            chmod 600 $PGDUMP
            pg_dump -h 127.0.0.1 -U archive -Fp -n public -O -x -d $i > $PGDUMP
        done
        ;;
esac
exit
