#!/bin/bash

function parse_yaml {
   local prefix=$2
   local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
   sed -ne "s|^\($s\):|\1|" \
        -e "s|^\($s\)\($w\)$s:$s[\"']\(.*\)[\"']$s\$|\1$fs\2$fs\3|p" \
        -e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p"  $1 |
   awk -F$fs '{
      indent = length($1)/2;
      vname[indent] = $2;
      for (i in vname) {if (i > indent) {delete vname[i]}}
      if (length($3) > 0) {
         vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
         printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);
      }
   }'
}

eval $(parse_yaml $1)

echo $webrootdir
echo $dbname
echo $dbhost
echo $dbuser
echo $dbpass
echo $target

#
# Website Backup Shell Script
# by Andrew Currie (andrew@digitalpci.com)
#
# Performs a full backup of the specified database and document root. 
# Be sure to edit the configuration options at the beginning of the file to match your environment prior to executing.
# The end result will be one TAR archive with the name 'website-backup-(current-datestamp)'. 
# The backup includes the entire document root directory and also a 'database.sql' file containing a MySQL dump of the database.
#
# Begin Configuration Options (Specify which webroot and which database to backup.)
#
# Define The Website Document Root
#  webrootdir=/home4/ecoleste/sites/cantine  
#
# Define The MySQL MySQL Database Name
#  dbname=ecoleste_cantinev2     
#
# Define The MySQL Database Connection 
#  dbhost=localhost
#  dbuser=ecoleste_cantine
#  dbpass=dHMqyNjmB9hC7j8S
#
# Define The Default Backup Filename
#  tarnamebase=Website-Backup-
  datestamp=`date +'%Y-%m-%d'`
#
# Define The Script Execution Point
  cd $outdir
  
  startdir=`pwd`
#
# Define The Temporary Working Directory
  tempdir=TemporaryBackup-$datestamp
#
# End Configuration Options
#
# Begin The Input Parameters Check
#

tarname=$target-$datestamp.tgz

#
# Begin the backup process.
#
echo "";
echo "******************************************";
echo " PREPARING TO BACKUP THE WEBSITE          ";
echo "******************************************";
echo "";
#
#
# Create The Temporary Working Directory
#
echo "";
echo "******************************************";
echo " SETTING UP A TEMPORARY BACKUP LOCATION   ";
echo "******************************************";
echo "";
#
mkdir $tempdir
echo "";
echo "******************************************";
echo " TEMPORARY BACKUP LOCATION SETUP COMPLETE ";
echo "******************************************";
echo "";
#
#
# Compress The Specified Document Root Into a TAR Archive 
echo "";
echo "**********************************************";
echo " COMPRESSING THE WEBSITE FILES IN $webrootdir ";
echo "**********************************************";
echo "";
cd $webrootdir
tar cf $startdir/$tempdir/$target.tar .
echo "";
echo "**************************************************";
echo " WEBSITE FILES ARE NOW COMPRESSED INTO AN ARCHIVE ";
echo "**************************************************";
echo "";
#
# Peform mysqldump on the database specified in configuration options
#
echo "";
echo "***********************************************";
echo " PREPARING TO BACKUP THE DATABASE $dbname      ";
echo "***********************************************";
echo "";
cd $startdir/$tempdir
mysqldump -p --user=$dbuser --password=$dbpass --host=$dbhost --add-drop-table $dbname > $dbname.sql
echo "";
echo "***********************************************";
echo " DATABASE BACKUP of $dbname COMPLETED          ";
echo "***********************************************";
echo "";
#
# Append The Website Files Archive with The MySQL Database Backup
#
echo "";
echo "***********************************************************";
echo " MERGING THE WEBROOT AND DATABASE BACKUPS INTO ONE ARCHIVE ";
echo "***********************************************************";
echo "";
tar czf $startdir/$tarname $target.tar $dbname.sql
echo "";
echo "***********************************************************";
echo " WEBSITE BACKUP COMPLETED ($tarname)                       ";
echo "***********************************************************";
echo "";
#
# Cleanup Temporary Files
#
echo "";
echo "***********************************************************";
echo " PERFORMING SOME CLEANUP OPERATIONS                        ";
echo "***********************************************************";
echo "";
cd $startdir
rm -r $tempdir
echo "";
echo "***********************************************************";
echo " CLEANUP OPERATIONS COMPLETED                              ";
echo "***********************************************************";
echo "";
#
# Display The Confirmation Notice
#
echo "";
echo "***********************************************************";
echo " FULL WEBSITE BACKUP COMPLETED                             ";
echo "***********************************************************";
echo "";
#

