mkBashckup v0.1

Nama Aplikasi:
mkBashckup

Versi Stabil:
0.1

Lisensi:
GPLv3

Dependensi:
mysql tar gzip ncftp

Deskripsi:
Script backup direktori dan mysql database berbasis BASH. Direktori yang dibackup adalah direktori yang ditentukan dan dikompres dalam format GZIP. Backup MYSQL dilakukan pada suatu database yang ditentukan dan dipecah-pecah untuk tiap tabel dalam format dump SQL dan XML yang terkompresi dalam format GZIP.

Source code:

#/bin/bash -l
#
# Author: Luthfi Emka
# Date: 28/11/2012
#
# Script name: mkbashckup.sh
#
#
# Backup Script purpose, to backup:
# 1. MySQL
# -create .sql and .xml dumps for each database separately
# 2. path's to log, public_html
# 3. upload to FTP folder
#

###USER MUST SET FOR SERVER ###
HOMENAME="Judul Backup"
BACKUP_DEST="/home/username/.backup"
MUSER="username"
MPASS="password"
MHOST="localhost"
DBNAME="nama database"
pathLOG="/home/username/logs"
pathWWW="/home/username/public_html/wp-content"
FTPU="user ftp" # ftp login name
FTPP="password ftp" # ftp password
FTPS="host ftp" # remote ftp server
FTPF="/direktori/ftp" # remote ftp server directory for $FTPU & $FTPP
#################################

DATE="$(which date)"
HOST=$(hostname|cut -f1 -d.)
currtimestamp=$($DATE +%Y-%m-%d--%H-%M-$HOST)
path="$BACKUP_DEST/${currtimestamp}"
oldbackups="$BACKUP_DEST/old"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
BAKMYSQLPATH="$path/mysql"
BAKDIRPATH="$path/directories"
GZIP="$(which gzip)"
TAR="$(which tar)"
CHMOD="$(which chmod)"
MKDIR="$(which mkdir)"
RM="$(which rm)"
ECHO="$(which echo)"
FIND="$(which find)"
MAIL="$(which mail)"

#####
##Find backups older than fifteen days, remove them
$MKDIR -p $oldbackups; $FIND $BACKUP_DEST -type d -mtime +15 -exec mv {} $oldbackups/ \;
$RM -Rf $oldbackups
#####

###CREATE BACKUP DIRECTORY IF IT DOESN'T EXIST, REMOVE IF IT DOES ###
[ ! -d $BAKMYSQLPATH ] && $MKDIR -p $BAKMYSQLPATH || $RM -f $BAKMYSQLPATH/*
[ ! -d $BAKDIRPATH ] && $MKDIR -p $BAKDIRPATH || $RM -f $BAKDIRPATH/*
$TAR -czvf $BAKDIRPATH/${currtimestamp}_log.tar.gz $pathLOG/
ncftpput -m -u $FTPU -p $FTPP $FTPS  $FTPF $BAKDIRPATH/${currtimestamp}_log.tar.gz
$TAR -czvf $BAKDIRPATH/${currtimestamp}_www.tar.gz $pathWWW/
ncftpput -m -u $FTPU -p $FTPP $FTPS  $FTPF $BAKDIRPATH/${currtimestamp}_www.tar.gz

cd $BAKMYSQLPATH

##List all databases
DBS="$($ECHO 'show tables;' | $MYSQL -u $MUSER -h $MHOST -p$MPASS --database $DBNAME | grep -v '^Tables_in_')"
$ECHO "Found databases:"
$ECHO $DBS

for db in $DBS
## DUMP each database in .sql and .xml format, compress
do
$MYSQLDUMP -u $MUSER -p$MPASS $DBNAME $db > $BAKMYSQLPATH/$db.sql
$MYSQLDUMP -u $MUSER -p$MPASS $DBNAME $db --xml > $BAKMYSQLPATH/$db.xml

$GZIP -9 $BAKMYSQLPATH/$db.sql
$GZIP -9 $BAKMYSQLPATH/$db.xml
mv $BAKMYSQLPATH/$db.sql.gz $BAKMYSQLPATH/${currtimestamp}_$db.sql.gz
ncftpput -m -u $FTPU -p $FTPP $FTPS  $FTPF $BAKMYSQLPATH/${currtimestamp}_$db.sql.gz
mv $BAKMYSQLPATH/$db.xml.gz $BAKMYSQLPATH/${currtimestamp}_$db.xml.gz
ncftpput -m -u $FTPU -p $FTPP $FTPS  $FTPF $BAKMYSQLPATH/${currtimestamp}_$db.xml.gz
done

$ECHO "DB Backup complete."
$ECHO "Backup files are located in $BAKMYSQLPATH"

Download:
http://emka.web.id/wp-content/uploads/2012/11/mkbashckup-v0.1.sh_.txt

About the Author

Ifhtul Emka

Petani Sistem Informasi, tukang las plugin & themes Wordpress. Co-Founder SistemInformasi.biz. Saat ini aktif sebagai konsultan development sistem informasi beberapa kampus, sekaligus Developer & kontributor di OpenMandriva Linux. Bekerja sehari-hari di ICT Center Unnes sebagai tukang sapu Programmer.

One comment

Leave a Reply

%d bloggers like this: