Friday, 17 February 2017

MEB3.12_bakup.sh




#!/bin/bash
#creates and maintains MySQL Enterprise Backup (MEB) backups
#prints usage
usage()
{
    echo version: "$VERSION"
    echo "
Usage: `basename $0` [command] [MEB options]
Commands:
    full                                make full backup
    incremental                         make incremental backup
    incremental-with-redo-log-only      make incremental backup with redo log only
    remove-old                          remove old backups
    "
}
# creates defaults values
initialize()
{
###############################################
#manual entries by user
    WORKINGDIR=/tmp
    user=root
    pass="google@123"
    dREMOVETIME=0
    VERSION=1.0
#Manual entries section ends
################################################
    MEBOPTIONS=
    CURDATE=`date +%y%m%d_%H%M%S`
#pre-defined defaults, can be overriden
    dMYSQLBACKUP="/usr/local/mysql/bin/mysqlbackup"
    dBACKUPDIR=$WORKINGDIR/FULLBACKUPDIR
    dINCREMENTALDIR=$WORKINGDIR/INCREMENTALBACKUPDIR
    dLOGDIR=$WORKINGDIR/log
    dBACKUPIMAGENAME=backup
    dOUTLOG=backup_output.log
    #clean failed backups
    dCLEANUP=0
    
}
if [[ ! -e /tmp/log ]]; then
            mkdir -p /tmp/log
fi
#parses options
parse_options()
{
    if [ -z "$1" ]
    then
        usage
        exit 1
    fi
    case $1 in
    full)                    COMMAND=do_full;;
    incremental)                      COMMAND=do_incremental;;
    incremental-with-redo-log-only)           COMMAND=do_incremental_with_redo_log_only;;
    remove-old)                      COMMAND=do_remove_old;;
    *)                     usage; exit 1;;
    esac
    shift
    MEBOPTIONS=$@
    
    #setting environment variables
    if [ -z $MYSQLBACKUP ]
    then
        MYSQLBACKUP=`which mysqlbackup`
        if [ -z $MYSQLBACKUP ]
        then
            MYSQLBACKUP=$dMYSQLBACKUP
        fi
    fi
    
    if [ -z $BACKUPDIR ]
    then
        BACKUPDIR=${dBACKUPDIR}
    fi
    
    if [ -z $INCREMENTALDIR ]
    then
        INCREMENTALDIR=${dINCREMENTALDIR}
    fi
    
    if [ -z $LOGDIR ]
    then
        LOGDIR=$dLOGDIR
    fi
    
    if [ -z $OUTLOG ]
    then
        OUTLOG=${dOUTLOG}.${CURDATE}
    fi
    
    if [ -z $REMOVETIME ]
    then
        REMOVETIME=$dREMOVETIME
    fi
    
    if [ -z $CLEANUP ]
    then
        CLEANUP=$dCLEANUP
    fi
    
}
#cleans up failed backups
cleanup()
{
    result=$?
    if [[ 0 -ne $result ]]
    then
        echo "Backup failed"
        if [[ 1 -eq $CLEANUP ]]
        then
        echo "Environment variable CLEANUP set, removing corrupted backup image"
            rm -f $1
        fi
        exit 1;
    else
        echo "Backup successful"
    fi
    if [[ 1 -eq $CLEANUP ]]
    then
        echo "Removing backup directory"
    fi
}
#makes full backup
do_full()
{
    $MYSQLBACKUP $MEBOPTIONS --backup-dir=$BACKUPDIR/${CURDATE} --compress --skip-binlog -u$user -p$pass backup >$LOGDIR/$OUTLOG 2>&1 
    cleanup $BACKUPDIR/$BACKUPIMAGENAME
}
#makes incremental backup
do_incremental()
{
    $MYSQLBACKUP $MEBOPTIONS --incremental --incremental-base=history:last_backup --incremental-backup-dir=$INCREMENTALDIR/${CURDATE} --skip-binlog  -u$user -p$pass  backup >$LOGDIR/$OUTLOG 2>&1
    cleanup $INCREMENTALDIR/$BACKUPIMAGENAME
}
#makes incremntal with redo long only backup
do_incremental_with_redo_log_only()
{
    $MYSQLBACKUP $MEBOPTIONS --incremental-with-redo-log-only --incremental-base=history:last_backup --incremental-backup-dir=$INCREMENTALDIR/${CURDATE} --skip-binlog  -u$user -p$pass backup >$LOGDIR/$OUTLOG 2>&1
    cleanup $INCREMENTALDIR/$BACKUPIMAGENAME
}
do_remove_old()
{
    find $BACKUPDIR/* -maxdepth 0 -type d -ctime +$REMOVETIME -exec rm -rf {} \;
    find $INCREMENTALDIR/* -maxdepth 0 -type d -ctime +$REMOVETIME -exec rm -rf {} \;
}
initialize
parse_options $@
$COMMAND
exit 0

No comments:

Post a Comment