#!/bin/bash
#------------------------------------------------------------ 
# Copyright (C) 2016 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------ 
#
# back up all of the databases
#
#

DATE=`date +%Y%m%d`
DOW=`date +%a`

FULLDOW='Sun'
BACKDIR="/app/backup/snapshots"
MYSQLDIR='/var/lib/mysql'

cd $BACKDIR

echo `date` "-- Started DB dump for $DATE"

for DB in `/usr/bin/mysql -e "show databases" | grep '^RSCOMMON$\|^C_\|^BP_'`
do
    # first, let's clean up any possible debris from a failure/crash.
    # this has happens rarely, but if it does, the rest of this will
    # fail forever for the affected database

    # while we're at it, let's be fairly explicit with the rm -rf.
    # if someone edits the script, loses track of what directory we're in,
    # whatever, that could be real bad (tm)
    rm -rf $BACKDIR/$DB

    # okay, so a change here is to do full backups on sundays and incrementals
    # every other day of the week. IF you should change this behavior, off machine
    # backups must be considered, as incrementals aren't much good without their
    # fulls. our retention strategy on amazon s3 is to keep weekly and monthly
    # copies for a long time, but to delete dailies after 14 days.
    # all of these mechanisms work on the sunday copy, so please, if you change
    # sundayness here, look at the cronjobs and the s3backup script
    if [ $DOW == $FULLDOW ]
    then
        mysql -e 'flush tables'
        touch $DB.lastfull
    fi

    # semaphore tables can be locked and they're empty anyway, so let's not
    # risk a deadlock (see FBoD16106) and omit them, we'll grab them another way.
    echo `date` "-- Backing up $DB"
    mysqlhotcopy $DB./~semaphore/ .
    
    if [ "0" != $? ];
    then
        echo "mysqlhotcopy returned an error, skipping $DB"
        continue
    fi

    # we'll grab the semaphore table like so. we just want them for restores.
    cp -p $MYSQLDIR/$DB/*semaphore* $DB/

    echo `date` "-- Gzipping $DB"

    # btw, we would have used tar's --listed-incremental switch, but that doesn't
    # work so well on directory copies as tar bases things on ctimes rather
    # than mtimes (which is more robust, just doesn't work for us)
    BACKTYPE='full'
    INCREMENTAL=''
    if [ -a $DB.lastfull ] && [ $DOW != $FULLDOW ]
    then
        BACKTYPE='partial'
        INCREMENTAL="--newer-mtime=./$DB.lastfull"
    fi

    # we're also splitting up tarballs into 1gb chunks to deal with amazon's 5gb
    # file limitation and to play nice in general. all you need to do is cat 'em
    # back together if you want your plain old tarballs back.
    # also, another thing to be careful with is filenames. the s3backup script
    # uses the date portion to figure out where to store the file (daily vs.
    # weekly/monthly directories). it looks for the date as the second element
    tar $INCREMENTAL -cz $DB | split --bytes=1000m -d - $DB.$DATE.$BACKTYPE.tgz.

    # normal cleanup, again be a bit more careful with recursive rm...
    rm -rf $BACKDIR/$DB
done

echo `date` "-- Finished DB dump for $DATE"
