|
backup version 0.592
把backup脚本分两个文件(backup.sh跟backupFunc.sh,请使用者把下述语句分开保存),现版本更具移植性。
首先,脚本脱离了uMntFtpDirs与01MntFtpDirs.sh的束缚,通过读取/etc/mtab,用函数实现了在备份系统时不用备份--bind的文件夹。
另外,本版本中不直接使用命令名进行操作,而使用变量来代指,一来便于用户在自己的机上使用--只需要更改LEADINGDIR(指向放置.tar.gz 文件的绝对路径)、PATH2BCKUPSH(指向backup.sh的绝对路径)跟$SUCOM后接的用户名的值就可以了。而且如果要提高脚本的安全性,可以把本脚本要用到的程序备份到一个独立目录,再把变量指向它,然后就算系统后来感染了,也还可以保持备份的安全性。
最后,本脚本还具备信号处理能力,可以防止用户不小心错按CTRL-C等发出TERM, INT信号,当然,用户如果真要结束脚本,只需要按提示应答即可。
同时,本人为本脚本程序源用GPL协议发布,用以更方便大家传播与使用。
------------------------------------backup.sh------------------------------------------------
#!/bin/sh
#
# "backup.sh" is a script written in bash to backup the whole system
# Copyright (C) 2003 alphatan<alphatan@263.net> version 0.592
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# You can get it by accessing http://www.gnu.org/licenses/gpl.html
#
declare -i whereAmI=0
# declare the script wide variables and definitions.
LEADINGDIR=/backup/filebck/LinuxBck
PATH2BCKUPSH=/etc/rc.d/init.d
AWKCOM=$(which awk)
CATCOM=$(which cat)
CHOWNCOM=$(which chown)
CPCOM=$(which cp)
ECHOCOM=$(which echo)
FINDCOM=$(which find)
LNCOM=$(which ln)
MKCOM=$(which mkdir)
MOUNTCOM=$(which mount)
RMCOM=$(which rm)
SUCOM=$(which su)
TARCOM=$($ECHOCOM $(which tar) -Pzcf )
TOUCHCOM=$(which touch)
UMOUNTCOM=$(which umount)
declare -a mountedDirsFromMtab=( $(grep '/var/ftp/' /etc/mtab) )
declare -i mountedDirsAmount=${#mountedDirsFromMtab[@]}
declare -f backupSystemDirs operatingMountDirs calculateTm signalDealingFunc
source ${0%/*.sh}/backupFunc.sh
trap signalDealingFunc INT TERM QUIT
# declare the copyright of this software
$ECHOCOM -ne "\n\n"
$ECHOCOM -ne "\t \"backup.sh\" is a script written in bash to backup the whole system \n"
$ECHOCOM -ne "\t Copyright (C) 2003 alphatan<alphatan@263.net> version 0.592\n"
$ECHOCOM -ne "\t This program is free software; you can redistribute it and/or modify \n"
$ECHOCOM -ne "\t it under the terms of the GNU General Public License as published by \n"
$ECHOCOM -ne "\t the Free Software Foundation; either version 2 of the License, or \n"
$ECHOCOM -ne "\t (at your option) any later version. \n"
$ECHOCOM -ne "\t You can get it by accessing http://www.gnu.org/licenses/gpl.html \n"
$ECHOCOM -ne "\n Input the backup dir name: "
read BACKUPDIR
# time calculating process starts
calculateTm start
let whereAmI=0
# backup system wide files
backupSystemDirs
let whereAmI=0
# Establish DETAILED and SIMPLE system list.
SYSLISTDIR=sysList
if [ ! -d $LEADINGDIR/$BACKUPDIR/$SYSLISTDIR ]; then
$MKCOM $LEADINGDIR/$BACKUPDIR/$SYSLISTDIR
fi
operatingMountDirs umount
let whereAmI=0
$FINDCOM / -fprint "$LEADINGDIR/$BACKUPDIR/$SYSLISTDIR/$BACKUPDIR""$SYSLISTDIR""Simple.txt"
$FINDCOM / -fls "$LEADINGDIR/$BACKUPDIR/$SYSLISTDIR/$BACKUPDIR""$SYSLISTDIR""Detailed.txt"
operatingMountDirs mount
let whereAmI=0
# Backup this script.
$CPCOM $PATH2BCKUPSH/backup*.sh $LEADINGDIR/$BACKUPDIR/
# Establish sysChange.log by "su alphatan" at $BACKUPDIR;
# Then, remove the former linkage, and "link" again at /home/alphatan;
$TOUCHCOM $LEADINGDIR/$BACKUPDIR/sysChange.log
$CHOWNCOM alphatan.root $LEADINGDIR/$BACKUPDIR/sysChange.log
$SUCOM alphatan --command="$RMCOM -rf ~/sysChange.log"
$SUCOM alphatan --command="$LNCOM -s $LEADINGDIR/$BACKUPDIR/sysChange.log ~/sysChange.log"
# "empty line to end syntax hightlight of \"
# Calculate consuming time.
calculateTm end
let whereAmI=0
------------------------------------backup.sh------------------------------------------------
------------------------------------backupFunc.sh------------------------------------------------
#
# "backupFunc.sh" is a script of functions written in bash as an associated "backup.sh"
# Copyright (C) 2003 alphatan<alphatan@263.net> version 0.59
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# You can get it by accessing http://www.gnu.org/licenses/gpl.html
#
# Following is the definitions of functions.
function calculateTm()
{
let whereAmI=1
if [ $1 = 'end' ]; then
ENDTIMEMARK=`date +%s`
let USEDSECONDS=$ENDTIMEMARK-$STARTTIMEMARK
let USEDSECOND=$USEDSECONDS%60
let USEDMINUTE=($USEDSECONDS/60)%60
let USEDHOUR=($USEDSECONDS/3600)%60
if [ $USEDSECONDS == 0 ]; then
$ECHOCOM "It doesn't need a second?? Is there something in trouble?"
builtin exit 1
else
$ECHOCOM "It used $USEDHOURUSEDMINUTEUSEDSECOND"
builtin exit 0
fi
elif [ $1 = 'start' ]; then
STARTTIMEMARK=`date +%s`
else
$ECHOCOM -e " calculateTM Usage: calculateTM [start|stop] " >&2
exit 1
fi
}
function operatingMountDirs()
{
let whereAmI=1
declare -i i=0
if [ $1 = mount ]; then
local OPERATIONONTHEMOUNTDIRS='$MOUNTCOM --bind ${mountedDirsFromMtab[$i]} ${mountedDirsFromMtab[$i+1]}'
elif [ $1 = umount ]; then
local OPERATIONONTHEMOUNTDIRS='$UMOUNTCOM ${mountedDirsFromMtab[$i+1]}'
else
$ECHOCOM -e " operatingMountDirs Usage: operatingMountDirs [mount|umount] " >&2
exit 1
fi
while [ $i -lt $mountedDirsAmount ] ; do
eval $OPERATIONONTHEMOUNTDIRS
let i+=6
done
}
function backupSystemDirs()
{
let whereAmI=1
if [ ! -d $LEADINGDIR/$BACKUPDIR ]; then
$MKCOM $LEADINGDIR/$BACKUPDIR
fi
$TARCOM $LEADINGDIR/$BACKUPDIR/bin.tar.gz /bin
$TARCOM $LEADINGDIR/$BACKUPDIR/boot.tar.gz /boot
$TARCOM $LEADINGDIR/$BACKUPDIR/dev.tar.gz /dev
$TARCOM $LEADINGDIR/$BACKUPDIR/etc.tar.gz /etc
$TARCOM $LEADINGDIR/$BACKUPDIR/home.tar.gz /home
$TARCOM $LEADINGDIR/$BACKUPDIR/initrd.tar.gz /initrd
$TARCOM $LEADINGDIR/$BACKUPDIR/lib.tar.gz /lib
$TARCOM $LEADINGDIR/$BACKUPDIR/misc.tar.gz /misc
$TARCOM $LEADINGDIR/$BACKUPDIR/opt.tar.gz /opt
$TARCOM $LEADINGDIR/$BACKUPDIR/root.tar.gz /root
$TARCOM $LEADINGDIR/$BACKUPDIR/sbin.tar.gz /sbin
$TARCOM $LEADINGDIR/$BACKUPDIR/tftpboot.tar.gz /tftpboot
operatingMountDirs umount
$TARCOM $LEADINGDIR/$BACKUPDIR/var.tar.gz /var
operatingMountDirs mount
$TARCOM $LEADINGDIR/$BACKUPDIR/usr_share.tar.gz /usr/share
$TARCOM $LEADINGDIR/$BACKUPDIR/usr.tar.gz --exclude=/usr/share /usr
}
function signalDealingFunc()
{
local userChoice
local lineNumber=$LINENO
if (( $whereAmI==0 )); then local currentOperatingFileName=$0 #/etc/rc.d/init.d/backup.sh
else local currentOperatingFileName=${0%.sh}Func.sh #/etc/rc.d/init.d/backupFunc.sh
fi
$ECHOCOM "Currently running LINE $LINENO within $currentOperatingFileName "
$CATCOM $currentOperatingFileName |$AWKCOM "{if (NR==$lineNumber || NR==$lineNumber-1 || NR==$lineNumber+1) print NR\":\ \"\$0;}"
builtin read -p "Are you sure you want to exit?(1:yes, 2:no) " userChoice
until [ -n "$($ECHOCOM $userChoice | egrep '^(1|2|y|yes|n|no)$')" ]; do
builtin read -p "Not a legal choice, (1:yes, 2:no) " userChoice
done
case $userChoice in
1 | y | yes ) builtin exit 1 ;;
*) return ;;
esac
}
------------------------------------backupFunc.sh------------------------------------------------ |
|