RSYNC SHELL SCRIPT WITH AUTOINCREMENTAL BACKUP


Beberapa hari yang lalu saya ditugaskan untuk membuat script backup untuk memindahkan file recording yang ada di server production ke server backup. Berikut adalah kebutuhannya:

1. File recording di simpan di /home/myapp/recording, format direktorynya /home/myapp/recording/yyyy/mm/dd
2. Jika space hardisk home sudah mencapai 75 % maka otomatis di lakukan purging dengan mensisakan data 7 hari terakhir

Lampiran script rsync

#!/bin/bash
# “PUSH” RECORDING (VOICELOGGER) to SERVER BACKUP
# ————————————————————————-
# Copyright (c) 2011
# ————————————————————————-
# 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/myapp/recording/”
BACKUP_DIR=”/home/myapp/recording/”
LOG_DIR=”/home/myapp/recording/log/”
CURRENT_LOG=”/home/myapp/recording/log/rsync-current.log”

PERCENTAGE_ALERT=”75″

PARTITION_DIR=”/dev/mapper/VolGroup00-LogVol00″ # partisi /home

TESTING=”N”

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=”root”
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 sukses”
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
#rsync -avr –stats –progress -e ‘ssh -p 22’ /home/myapp/recording/ myapp@10.10.21.24:/home/myapp/recording/
RETVAL=$?
if [ $RETVAL -eq “0” ]; then
echo “RETVAL :”. $RETVAL
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
echo “Delete Dir $SOURCE_DIR$d” >> $LOG_FILE
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

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: