#!/bin/bash
# Shell script to backup concrete5 (database and /files/)
#

##########################
# CONFIGURATION
##########################

#
# DESTDIR - destination directory for backups. Backups will be stored there
# no trailing slash
DESTDIR="/home/user/backups"

#
# CONCRETE5_DIR - directory of concrete5 (where it is located)
# no trailing slash
CONCRETE5_DIR="/home/user/public_html"


#
# Database config
# If you don't remember this, check /config/site.php of your concrete5 installation
#

# Database User (see DB_USERNAME in config/site.php)
MYSQL_USER="concrete5"
# Database password (see DB_PASSWORD in config/site.php)
MYSQL_PASSWORD="concrete5"
# Database name (see DB_DATABASE in config/site.php)
MYSQL_DATABASE="concrete5"
# Database Host (see DB_SERVER in config/site.php)
MYSQL_HOST="localhost"

###############################################################################
# DO NOT EDIT THE CODE BELOW UNLESS YOU KNOW WHAT YOU ARE DOING               #
###############################################################################

# detecting paths to executables
TARPATH="$(which tar)"
MYSQLDUMPPATH="$(which mysqldump)"
GZIPPATH="$(which gzip)"

# Get date in yyyy-mm-dd format
# (this format is convenient to sort the folders by name)
TODAY="$(date +"%Y-%m-%d")"


# create folder in backups/, for example: backups/2010-10-25/
DESTPATH="$DESTDIR/$TODAY"
mkdir $DESTPATH

# Destination file for the files backup
FILES_DESTFILE="$DESTPATH/files.$TODAY.tgz"
# Destination file for the database backup
DB_DESTFILE="$DESTPATH/db.$TODAY.gz";

# backuping database
$MYSQLDUMPPATH -u $MYSQL_USER -h $MYSQL_HOST -p$MYSQL_PASSWORD $MYSQL_DATABASE | $GZIPPATH -9 > $DB_DESTFILE

# backuping /files/
FILES_TO_BACKUP="$CONCRETE5_DIR/files"
$TARPATH -czf $FILES_DESTFILE $FILES_TO_BACKUP