#!/bin/sh
PATH=/usr/local/bin:/usr/local/sbin:~/bin:/usr/bin:/bin:/usr/sbin:/sbin
ISO_TIMESTAMP=`date +"%Y-%m-%dT%H:%M:%S"`

# This script backups MySQL Databases to remote timestamped target file mysql_databases.sql.gz.
# Please change the variables in step (1) first before running the script.

# (1) set up all backup variables
COMMANDER_HOME=/path/to/crossftp-all-bin
MYSQL_USER=username
MYSQL_PASS=password
SOURCE_FILE_PATH=/tmp/mysql_databases.sql.$ISO_TIMESTAMP
TARGET_SITE_ID=ID
TARGET_FILE_PATH=/path/to/backup/mysql_databases.sql.$ISO_TIMESTAMP

# (2) dump all MySQL databases
echo "Dump MySQL databases to file ${SOURCE_FILE_PATH}"
# use this command for a database server on localhost. add other options if need be.
mysqldump --opt --user=${MYSQL_USER} --password=${MYSQL_PASS}  --host=localhost --port=3306 --all-databases --result-file=$SOURCE_FILE_PATH

# (3) gzip the mysql database dump file
echo "Compress ${SOURCE_FILE_PATH} to ${SOURCE_FILE_PATH}.gz"
gzip $SOURCE_FILE_PATH

# (4) backup the MySQL database dump file
echo "Backup ${SOURCE_FILE_PATH}.gz to remote file ${TARGET_FILE_PATH}.gz"
cd $COMMANDER_HOME && ./commander.sh --action copy --sourceSite MyComputer --sourceFile ${SOURCE_FILE_PATH}.gz --targetSite $TARGET_SITE_ID --targetFile ${TARGET_FILE_PATH}.gz

# (5) cleanup tempoary dump file
unalias rm     2> /dev/null
rm ${SOURCE_FILE_PATH}     2> /dev/null
rm ${SOURCE_FILE_PATH}.gz  2> /dev/null

# (6) show user the result
echo "Finished backup ${SOURCE_FILE_PATH} to remote file ${TARGET_FILE_PATH}.gz"
