Jumat, 26 Maret 2010

Configure Tape Backup di Centos

Berikut ini saya berikan script untuk Backup data dengan Tape Backup DAT72.
Adapaun data-data yang ada di server kita backup ke dalam tape backup secara Full backup dan Incremental. Untuk Incremental dilakukan dari hari senin sampai dengan Kamis. dan Jumat dilakukan Full Backup.

Setelah anda buat script backup ini maka anda tinggal menambahkan command ini di schedule.

0 23 * * 1-5 /usr/local/bin/mybackup.sh

Note: Dijalankan pada jam 23.00 dan dilakukan pada hari 1-5

Berikut script untuk backup.


#!/bin/bash
# A UNIX / Linux shell script to backup dirs to tape device like /dev/st0 (linux)
# This script make both full and incremental backups.
# You need at two sets of five tapes. Label each tape as Mon, Tue, Wed, Thu and Fri.
# You can run script at midnight or early morning each day using cronjons.
# The operator or sys admin can replace the tape every day after the script has done.
# Script must run as root or configure permission via sudo.
# -------------------------------------------------------------------------
# Copyright (c) 1999 Vivek Gite
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
# Last updated on : March-2003 - Added log file support.
# Last updated on : Feb-2007 - Added support for excluding files / dirs.
# -------------------------------------------------------------------------
# Changed with centralized configuration - Jan 2010 by Toto
#
# Configuration file default
# default : /etc/mybackup/backup.conf
CONF=/etc/mybackup/backup.conf
. $CONF

BACKUP_ROOT_DIR=($(cat $BACKUP_DIR_FILE))
NOW=$(date +"%a")

# Backup Log file
LOGFIILE=$LOGBASE/$NOW.backup.log

# ------------------------------------------------------------------------
# Excluding files when using tar
# Create a file called $EXCLUDE_CONF using a text editor
# Add files matching patterns such as follows (regex allowed):
# home/vivek/iso
# home/vivek/*.cpp~
# ------------------------------------------------------------------------
[ -f $EXCLUDE_CONF ] && TAR_ARGS="-X $EXCLUDE_CONF"

#### Custom functions #####
# Checking the backup result
backup_checking(){
if [ $2 == "end" ];
then
if [ $3 -eq 0 ];
then
kondisi="backup succeed"
else
kondisi="backup failed"
fi
else
kondisi="backup start"
fi
echo "$(date +%d'-'%b'-'%Y' '%H':'%M) $1 $kondisi" >> $LOGBASE/backup-attempt.log
}

# Make a full backup
full_backup(){
local old=$(pwd)
cd /
backup_checking Full start
rm -f $INC_FILE && $TAR $TAR_ARGS -cvpf $TAPE ${BACKUP_ROOT_DIR[*]} -g $INC_FILE
backup_checking Full end $?
$MT -f $TAPE rewind
$MT -f $TAPE offline
cd $old
}

# Make a partial backup
partial_backup(){
local old=$(pwd)
cd /
backup_checking Incremental start
$TAR $TAR_ARGS -cvpf $TAPE ${BACKUP_ROOT_DIR[*]} -g $INC_FILE
backup_checking Incremental end $?
$MT -f $TAPE rewind
$MT -f $TAPE offline
cd $old
}

# Make sure all dirs exits
verify_backup_dirs(){
local s=0
for d in ${BACKUP_ROOT_DIR[*]}
do
if [ ! -d /$d ];
then
#echo "Error : /$d directory does not exits!"
echo "$(date +%d'-'%b'-'%Y' '%H':'%M) Error : /$d directory does not exits!" >> $LOGBASE/backup-attempt.log
s=1
fi
done
# if not; just die
[ $s -eq 1 ] && exit 1
}

#### Main logic ####

# Make sure log dir exits
[ ! -d $LOGBASE ] && $MKDIR -p $LOGBASE

# Verify dirs
verify_backup_dirs

# Okay let us start backup procedure
# If it is monday make a full backup;
# For Tue to Fri make a partial backup
# Weekend no backups
case $NOW in
Fri) full_backup;;
Mon|Tue|Wed|Thu) partial_backup;;
*) ;;
esac > $LOGFIILE 2>&1
# EOF

Dan Report dari hasil backup ada di backup-attempt.log

Selamat Mencoba

0 komentar: