Shell Scripts

Shell Script to backup Couchbase server using cbbackupmgr :

#!/bin/bash
###################################################################
#       Script Name     :       cbbackupmgr.sh
#       Date            :       27/08/2019
#       Author          :       Database Team
#       Description     :       This script will take the backup of Couchbase cluster.
#       Backup Strategy as follows :
#                               1. Weekdays Differential Backup
#                               2. Weekend one day Full Backup (Sunday)
#                               3. Remove the past week backup before starting the Fresh Backup
###################################################################
BACKUP_PATH=/backups/
SCRIPT_PATH=/backups/scripts
CBBACKUPMGR=/opt/couchbase/bin/cbbackupmgr
sDate=`date +%Y%m%d`;
DAY_OF_WEEK=`date +%a`
LOG_DATE=`date +%Y%m%d%H%M`
STATUS=1
HOST=`hostname`
RTUSER=`id |cut -d"(" -f2 | cut -d ")" -f1`
REMOVE_DAY=
CLUSTER_NODE_URL=http://Your_Server_Name_OR_IP:8091
FBU_DAY=Sun
DBU=Administrator
DBP='Admin_Pa$$word'
Number=7
current_date=$(date +%Y%m%d)
past_date=$(date -d "$current_date - $Number days" +%Y%m%d) #1 week
while [ ! -d $BACKUP_PATH/$past_date ] ;
do
past_date=$(date -d "$past_date - 1 days" +%Y%m%d)
done
if [ "$DAY_OF_WEEK" = "$FBU_DAY" ]
then
echo $sDate > $SCRIPT_PATH/current_week_prod.txt;
CBB_MODE=full
CBB_LOG_FILE=${BACKUP_PATH}/logs/couchbase_backup_${CBB_MODE}_${LOG_DATE}.log
echo >> $CBB_LOG_FILE
chmod 666 $CBB_LOG_FILE
echo Runtime Script: $0 >> $CBB_LOG_FILE
echo Runtime User: $RTUSER >> $CBB_LOG_FILE
echo PID: $$ >> $CBB_LOG_FILE
echo Hostname: $HOST >> $CBB_LOG_FILE
echo Couchbase User: $DBU >> $CBB_LOG_FILE
echo ==== started on `date` ==== >> $CBB_LOG_FILE
echo >> $CBB_LOG_FILE
echo Backup Mode: $CBB_MODE >> $CBB_LOG_FILE
$CBBACKUPMGR remove --archive $BACKUP_PATH --repo $past_date #Removes Past week backup
$CBBACKUPMGR config --archive $BACKUP_PATH --repo $sDate
$CBBACKUPMGR backup --archive $BACKUP_PATH --repo $sDate --cluster $CLUSTER_NODE_URL -u $DBU -p $DBP --threads 4
STATUS=$?
else
CBB_MODE=diff
CBB_LOG_FILE=${BACKUP_PATH}/logs/couchbase_backup_${CBB_MODE}_${LOG_DATE}.log
echo >> $CBB_LOG_FILE
chmod 666 $CBB_LOG_FILE
echo Runtime Script: $0 >> $CBB_LOG_FILE
echo Runtime User: $RTUSER >> $CBB_LOG_FILE
echo PID: $$ >> $CBB_LOG_FILE
echo Hostname: $HOST >> $CBB_LOG_FILE
echo Couchbase User: $DBU >> $CBB_LOG_FILE
echo ==== started on `date` ==== >> $CBB_LOG_FILE
echo >> $CBB_LOG_FILE
echo Backup Mode: $CBB_MODE >> $CBB_LOG_FILE
sCurrentWeek=`head $SCRIPT_PATH/current_week_prod.txt`
sCurrentRepo=$sCurrentWeek
echo $sCurrentRepo
$CBBACKUPMGR backup --archive $BACKUP_PATH --repo $sCurrentRepo --cluster $CLUSTER_NODE_URL -u $DBU -p $DBP --threads 2
STATUS=$?
fi
if [ "$STATUS" = "0" ]
then
    LOGMSG="Ended successfully"
else
    LOGMSG="Ended in error"
fi

echo >> $CBB_LOG_FILE
echo Runtime Script: $0 >> $CBB_LOG_FILE
echo ==== $LOGMSG on `date` ==== >> $CBB_LOG_FILE
echo >> $CBB_LOG_FILE

if [ $STATUS -ne 0 ]
then
cat $CBB_LOG_FILE | /bin/mailx -s "$HOST: Couchbase Database Backup Failed" Your_Email_ID@gmail.com
fi;

Review
4

About the author

BestTechReads

Add Comment

Click here to post a comment

Categories

Categories