Backupを色々考えていたら、望むようなBackup取得手法が見当たらず、ついかっとなって作ってしまった。後悔はしていない。
今回のBackup Scriptは、以下を前提にしている
要するに、OSが入ったVMが立ち上げ、必要なpackage(pkgsrcやyum)を導入してしまえば、Backupからの書き戻しで概ねOKになるようにしたということ。 但し、Linux(CentOS)の場合、単純に/etc/を上書きしてよいかは、疑問が残る。
expr $var + n を使う手も考えたが、NetBSDの/bin/shやCentOS上のbashは $ ( ( expression ) ) 記法が使えるので、とりあえずこれで。言うまでもないことですが、一応。
このWikiに掲載されている全てのコンテンツの例に漏れず、本Scriptは無保証である。従って、本Scriptを利用したもしくは利用できないに関わらず、作者は一切の責を負わない。
#! /bin/sh
# @(#) This script is my personal backup script.
#
# Personal Backup Script.
#
# Written by seirios@seirios.org
#
# Need bkup.conf.
#
# ***WARNING***
# This script runs on NetBSD 6 or CentOS 6.
#
# $Id:$
#
echo "System backup start."
##### Prepare to run.
# DEBUGGING.
#DEBUG=1
# Under debugging, only show all command line. not execute
if [ "x"$DEBUG = "x" ]; then
EVAL="eval"
else
EVAL="echo"
fi
CONF=./bkup.conf
# Tempolary file.
CONFTMP=".conftmp"; [ -f $CONFTMP ] && rm -f $CONFTMP
BKDIR="./.bkdir"; [ -f $BKDIR ] && rm -f $BKDIR
BKMYSQL="MYSQL"; [ -f $BKMYSQL ] && rm -f $BKMYSQL
UNAME=`which uname`; [ "x"$DEBUG != "x" ] && echo "uname : $UNAME"
CAT=`which cat`; [ "x"$DEBUG != "x" ] && echo "cat : $CAT"
GREP=`which grep`; [ "x"$DEBUG != "x" ] && echo "grep : $GREP"
CUT=`which cut`; [ "x"$DEBUG != "x" ] && echo "cut : $CUT"
TAR=`which tar`; [ "x"$DEBUG != "x" ] && echo "tar : $TAR"
GZIP=`which gzip`; [ "x"$DEBUG != "x" ] && echo "gzip : $GZIP"
MV=`which mv`; [ "x"$DEBUG != "x" ] && echo "mv : $MV"
MYSQLDUMP=`which mysqldump`; [ "x"$DEBUG != "x" ] && echo "mysqldump : $MYSQLDUMP"
OSNAME=`$UNAME -s`;
ROTATE="" # Log rotate generation.
MYSQL="0" # Backup MySQL?
MYSQLPASS="" # MySQL Password
##### Parse commandline argument.
[ $# -gt 1 ] && echo "Too match argument. exit." && exit
[ $# -eq 1 ] && CONF=${1}
[ ! -f $CONF ] && echo "Configuration file is not found. exit." && exit
[ "x"$DEBUG != "x" ] && echo "Conf : $CONF"
##### Parse configuration file.
# ***WARNING***: while loop must runs current shell(not subshell), so runs exec.
$CAT $CONF | $GREP -v '^#' | $GREP -v '^$' > $CONFTMP
exec < $CONFTMP
while read line; do
FLAG=`echo ${line} | $CUT -d " " -f 1`
INIT=`echo ${line} | $CUT -b 1`
if [ $INIT = "[" ]; then
[ $FLAG = "[CONF]" ] && BLOCK="CONF" && continue
[ $FLAG = "[FILE]" ] && BLOCK=$BKDIR && continue
[ $FLAG = "[MYSQL]" ] && BLOCK=$BKMYSQL && continue
echo "Unknown BLOCK. Check Configuration." && exit
fi
[ x"$BLOCK" = "x" ] && echo "BLOCK is invalid. Check Configuration." && exit
# Configuratin.
if [ $BLOCK = "CONF" ]; then
[ $FLAG = "LOGGEN" ] && ROTATE=`echo ${line} | $CUT -d " " -f 2`
[ $FLAG = "BKUPDIR" ] && BKUPDIR=`echo ${line} | $CUT -d " " -f 2` && \
[ ! -d $BKUPDIR ] && echo "Backup Dir is not exist" && exit
fi
# File BLOCK
[ $BLOCK = $BKDIR ] && [ $FLAG -eq 1 ] && \
echo "`echo ${line} | $CUT -d " " -f 2`" >> $BLOCK
# MySQL BLOCK
[ $BLOCK = $BKMYSQL ] && [ $FLAG -eq 1 ] && \
MYSQL=1
MYSQLPASS=`echo ${line} | $CUT -d " " -f 2`
done
[ "x"$DEBUG != "x" ] && \
echo "OS name : $OSNAME" && \
echo "Log gen : $ROTATE" && \
echo "MySQL Bkup: $MYSQL" && \
echo "MySQL Pass: $MYSQLPASS" && \
echo "Target Dir: see $BKDIR";
##### Rotate Backup File
[ "x"$DEBUG != "x" ] && echo "===== Rotate Backup ====="
ROTATE=$(( $ROTATE - 1 ))
while (true); do
ROTATEFROM=$(( $ROTATE - 1 ))
[ -f $BKUPDIR/bkup.tgz.$ROTATEFROM ] && \
$EVAL "$MV $BKUPDIR/bkup.tgz.$ROTATEFROM $BKUPDIR/bkup.tgz.$ROTATE"
[ -f $BKUPDIR/pkg.txt.$ROTATEFROM ] && \
$EVAL "$MV $BKUPDIR/pkg.txt.$ROTATEFROM $BKUPDIR/pkg.txt.$ROTATE"
[ -f $BKUPDIR/mysqldump.sql.gz.$ROTATEFROM ] && \
$EVAL "$MV $BKUPDIR/mysqldump.sql.gz.$ROTATEFROM $BKUPDIR/mysqldump.sql.gz.$ROTATE"
ROTATE=$ROTATEFROM
[ $ROTATE -lt 2 ] && break
done
[ -f $BKUPDIR/bkup.tgz ] && \
$EVAL "$MV $BKUPDIR/bkup.tgz $BKUPDIR/bkup.tgz.1"
[ -f $BKUPDIR/pkg.txt ] && \
$EVAL "$MV $BKUPDIR/pkg.txt $BKUPDIR/pkg.txt.1"
[ -f $BKUPDIR/mysqldump.sql.gz ] && \
$EVAL "$MV $BKUPDIR/mysqldump.sql.gz $BKUPDIR/mysqldump.sql.gz.1"
##### File backup
[ "x"$DEBUG != "x" ] && echo "===== File Backup ====="
$EVAL "$TAR -cz -T $BKDIR -f $BKUPDIR/bkup.tgz"
##### Installed package list
[ "x"$DEBUG != "x" ] && echo "===== Get package List ====="
[ $OSNAME = "NetBSD" ] && $EVAL "/usr/sbin/pkg_info -a > $BKUPDIR/pkg.txt"
[ $OSNAME = "Linux" ] && $EVAL "/usr/bin/yum list installed > $BKUPDIR/pkg.txt"
##### MySQL backup.
[ "x"$DEBUG != "x" ] && echo "===== MySQL Backup ====="
if [ $MYSQL != "0" -a "x"$MYSQLDUMP != "x" -a "x"MYSQLPASS != "x" ]; then
$EVAL "$MYSQLDUMP -u root -A -a -e --password=\"$MYSQLPASS\" | $GZIP -9c > $BKUPDIR/mysqldump.sql.gz"
fi
##### Cleanup
[ "x"$DEBUG != "x" ] && [ -f $CONFTMP ] && rm -f $CONFTMP
[ "x"$DEBUG != "x" ] && [ -f $BKDIR ] && rm -f $BKDIR
[ "x"$DEBUG != "x" ] && [ -f $BKMYSQL ] && rm -f $BKMYSQL
echo "done."
# # System Backup Configuration File. # This is part of bkup.sh script. # # Written by seirios@seirios.org # # Syntax # flag [TAB]+ context # flag: 1 = Target, 0 = Not target. # System Configuration. [CONF] LOGGEN 7 # Not rotate if 0 BKUPDIR /nfs/bup001/bkup # Backup Storage # File block: Context = target Dir or file # [FILE] 1 /etc 1 /usr/pkg/etc 1 /var 1 /home 0 /export/home # MySQL Block: Context = MySQL connection password # Must need MySQL Administrator(such as root) Password. # *** WARNING *** # Need MYSQL ADMINISTRATOR PASSWORD, not System root password. # [MYSQL] 0 MySQL_PASSWORD