RSYN BACKUP :)


#!/bin/bash
# "PUSH" RECORDING (VOICELOGGER) to SERVER BACKUP
# -------------------------------------------------------------------------
# Copyright (c) 2011 <moses.spackeu@yahoo.com / mustafa@intelix.co.id>
# -------------------------------------------------------------------------
# Tested under RHEL / CentOS 5.5

FIND="$(which find)"
RSYNC="$(which rsync)"
DATE=$( date +%F )				 				 # format yyyy-mm-dd ex: 2011-30-03
DATE_TIME="$( date +%F)":"$( date +%H:%M:%S)"    # format yyyy-mm-dd ex: 2011-30-03 10:00:00
FORMAT_DIR="$( date +%Y/%m/%d)"			 		 # format yyyy/mm/dd ex: 2011/30/03

SOURCE_IP="10.10.21.23"
BACKUP_IP="10.10.21.24"

SOURCE_DIR="/home/ecentrix/recording/"
BACKUP_DIR="/home/ecentrix/recording/"
LOG_DIR="/home/ecentrix/recording/log/"
CURRENT_LOG="/home/ecentrix/recording/log/rsync-current.log"

PERCENTAGE_ALERT="75"

PARTITION_DIR="/dev/sda5"	# partisi /home

TESTING="Y"

LOG_FILE="${LOG_DIR}recording_$DATE.log"
if [ "$TESTING" == "Y" ]; then
	OPTIONS="-avrn --stats --progress -e"
else
	OPTIONS="-avr --stats --progress -e"
fi

SSH_USER="ecentrix"
SSH_HOST="${BACKUP_IP}"

> $CURRENT_LOG
> $LOG_FILE

REMOTE_IS_LIVE=`ping -s 1 -c 1 $BACKUP_IP > /dev/null; echo $?`
if [ $REMOTE_IS_LIVE -eq 0 ]; then
	SDATE=`date`
	echo "----  Ping server backup ${BACKUP_IP} at $SDATE  ---"  >> $CURRENT_LOG 2>&1
        $RSYNC $OPTIONS 'ssh -p 22' ${SOURCE_DIR} ${SSH_USER}@${SSH_HOST}:${BACKUP_DIR} >> $CURRENT_LOG 2>&1
        RETVAL=$?
        if [ $RETVAL -eq "0" ]; then
                echo "----  Rsync started at `date`  ---"  >> $CURRENT_LOG 2>&1
                echo "----  Rsync completed at `date`  ----" >> $CURRENT_LOG 2>&1
                grep "Rsync started at" $CURRENT_LOG >> $LOG_FILE
                grep "Number of files:" $CURRENT_LOG >> $LOG_FILE
                grep "Number of files transferred:" $CURRENT_LOG >> $LOG_FILE
                grep "Total file size:" $CURRENT_LOG >> $LOG_FILE
                grep "Total transferred file size:" $CURRENT_LOG >> $LOG_FILE
                grep "Total bytes sent:" $CURRENT_LOG >> $LOG_FILE
                grep "Total bytes received:" $CURRENT_LOG >> $LOG_FILE
                grep "Rsync completed at" $CURRENT_LOG >> $LOG_FILE
				df -PH | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;
				do
					LEFT_PERCENTAGE=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
					FS_PARTITION=$(echo $output | awk '{ print $2 }' )
					if [ "{$FS_PARTITION}" = "{$PARTITION_DIR}" ]; then 
						if [ "$LEFT_PERCENTAGE" -ge "$PERCENTAGE_ALERT" ]; then
							echo "" >> $LOG_FILE
							#---------------delete backup 7 hari yang lalu----------------------------------------
							DAYS=$(for d in $(seq 7 14);do date --date="$d days ago" +"%Y/%m/%d"; done)
							for d in $DAYS
							do
								if [ "$TESTING" == "N" ]; then
									/bin/rm -rf $SOURCE_DIR$d
								fi
								echo "Delete Dir $SOURCE_DIR$d" >> $LOG_FILE
							done
							#---------------end 7 hari yang lalu-------------------------------------------

						fi
					fi
				done
        else
                echo "----  Rsync Error at `date` ----" >> $CURRENT_LOG 2>&1
                echo "----  Rsync Error at `date` ----" >> $LOG_FILE
                echo "----  Please check the error log ----" >> $LOG_FILE
        fi
else
        echo "---- Ping Server Backup ($BACKUP_IP) : FAIL, Pls Check Backup Server!!!!" >> $CURRENT_LOG 2>&1
fi
Advertisements

Tagged:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

shisdew

Listens until think alike

moses.spaceku@yahoo.com / voip ipbx

Hosted PBX, IP-PBX SOHO/ CALL CENTER, VOICE GATEWAY, VOICE CARD, COST EFECTIVE SOLUTIONS (LCR), GSM/CDMA GATEWAY

%d bloggers like this: