#USB Storage Utility *** 信用社业务数据U盘备份程序1.0
#(c) Copyright 2004
#编辑修改: 查明胜 zerozha@yahoo.com.cn
#==========================环境参数设置========================
#储蓄路径
BANKSTAR_HOME=/usr/bankstar
#对公路径
BANKACC_HOME=/usr/bankacc
#U盘系统装载点
MOUNT_POINT=/mnt
#U盘设备名称
DEV=/dev/dsk/1sC
#U盘备份保留天数
DAYS=30
#==========================结束========================
MONTH=
DAY=
#=============================更改路径
change_to_dir() {
dir=$1
[ -f $dir ] && rm -r $dir
[ ! -d $dir ] && mkdir $dir
cd $dir
}
#==============================end
#==============================删除逾期数据
DeleteYqData()
{
Num=`ls $1/*.dat | wc -l`
GeS=`expr $Num - $DAYS`
[ $GeS -gt 0 ] && {
Num=0
for i in `ls $1/*.dat`
do
Num=`expr $Num + 1`
yes | rm $i >/dev/null 2>/dev/null
[ $? -ne 0 ] && {
echo "n[2;7;4m$i 删除失败 ![2;7;0m"
}
[ $Num -ge $GeS ] && break
done
}
}
#=================================结束
GetYN()
{
while echo "n[2;7;4m$* (Y/N): [2;7;0m 7c"
do
read yn
case $yn in
[yY]) return 0
[nN]) return 1
*) echo " 7 7"
esac
done
}
#================================================
DateAnalyse()
{
value=`echo $1 | awk 'BEGIN { FS="/" }
{
Year=$1
Mon=$2
Day=$3
if(Year<1899 || Year >9999 || Mon<1 || Mon >12 ||
Day<1 || Day >31 || !IsDigit(Mon) || !IsDigit(Year) ||
!IsDigit(Day) || !IsYMD(Year,Mon,Day))
printf("1")
else printf("0")
}
function IsDigit(str)
{
for(i=1;i<=length(str);i++)
{
bit=substr(str,i,1)
if(bit<0 || bit>9) return(0)
}
return(1)
}
function IsLeapYear(YearNum)
{
if((int(YearNum%4)==0 && int(YearNum%100)!=0)||int(YearNum%400)==0)
return(1)
else return(0)
}
function IsYMD(YearNum,MonNum,DayNum)
{
DayArr["01"]=31
if(IsLeapYear(YearNum)) DayArr["02"]=29
else DayArr["02"]=28
DayArr["03"]=31
DayArr["04"]=30
DayArr["05"]=31
DayArr["06"]=30
DayArr["07"]=31
DayArr["08"]=31
DayArr["09"]=30
DayArr["10"]=31
DayArr["11"]=30
DayArr["12"]=31
if(DayNum>0 && DayNum<=DayArr[MonNum])
return(1)
else return(0)
}' `
return $value
}
#========================================
GetDate()
{
while true
do
echo "n请输入$* (yyyy/mm/dd): c"
read date
len=`expr length "$date"`
DateAnalyse $date
[ $? -ne 0 -o $len -ne 10 ] && {
echo " 7 7 7"
echo "[2;7;4m日期错误,按回车键重新输入 ...[2;7;0mc"
read key
continue
}
GetDateYN "输入$*为: [2;1;7m $date [2;7;0m, 是否正确"
[ $? -eq 0 ] && break
done
}
#=============================================
GetDateYN()
{
while echo "n 7 7$* (Y/N): c"
do
read yn
len=`expr length "$yn"`
[ $len -ne 1 ] && continue;
case $yn in
[yY]) return 0
[nN]) return 1
*) echo " 7c"
esac
done
}
#=============================================程序主画面
main_menu() {
clear;
tput cup 1 1
echo "[2;7;4m 安庆市城郊信用合作联社业务数据U盘备份程序 Ver 1.0 [2;7;0mc"
echo "n==========================================================c"
tput cup 3 1
echo "[1][2;7;1m 储蓄业务数据备份 [2;7;0m [2][2;7;1m 储蓄业务数据恢复 [2;7;0m"
tput cup 5 1
echo "[3][2;7;1m 对公业务数据备份 [2;7;0m [4][2;7;1m 对公业务数据恢复 [2;7;0m"
tput cup 7 1
echo "[5][2;7;1m 查看U盘使用情况 [2;7;0m [6][2;7;1m 删除U盘全部数据 [2;7;0m"
tput cup 9 1
echo "[7][2;7;1m 选择删除U盘数据 [2;7;0m [8][2;7;1m 列U盘内容 [2;7;0m"
tput cup 11 1
echo "[q][2;7;1m退 出 [2;7;0m"
tput cup 13 1
echo "n=========================================================="
echo " 现在时间:"`date '+%Y年%m月%d日'`",U盘数据将保留"$DAYS"天"
echo "=========================================================="
echo "请选择:c";
}
#=============================================END
check_privilege() {
id_num=`id -u `
if [ id_num -ne 0 ]
then
echo "请用超级用户执行此程序!!!!"
sleep 3
exit
fi
}
#============================================查看U盘使用情况
usd_statistics() {
total=`df -kv $DEV |grep $DEV| awk '{print $3}'`
used=`df -kv $DEV |grep $DEV| awk '{print $4}'`
free=`df -kv $DEV |grep $DEV| awk '{print $5}'`
percent=`df -kv $DEV |grep $DEV|awk '{print $6}'`
echo " U 盘 当 前 使 用 情 况 "
echo " --------------------------------------------------------------------"
echo " 总容量(K) 已用(K) 剩余(K) 百分比"
echo " ${total} ${used} ${free} ${percent}"
echo " --------------------------------------------------------------------"
echo " 按回车键继续......c"
read any
}
#============================================end
#============================================删除U盘所有数据
delete_all() {
echo "你确定要删除U盘所有数据吗?(y/n)c"
cd $MOUNT_POINT
read opt
if [ $opt = "y" ]
then
rm -rf *
fi
}
#============================================end
#============================================选择删除U盘数据
delete_single() {
cd $MOUNT_POINT
echo "U盘当前目录:"
lx
echo "请输入要删除目录:c"
read delete_dir
if [ -d $delete_dir -o -f $delete_dir ]
then
echo "你确定要删除${delete_dir}吗?(y/n)c"
read opt
if [ $opt = "y" ]
then
rm -rf $delete_dir
fi
else
echo "<$delete_dir>目录或文件不存在"
sleep 2
fi
}
#=========================================================
list_file() {
cd $MOUNT_POINT
echo "U盘当前目录:"
l
echo "任一键返回"
read any
}
backup_bankstar()
{
rm -r /tmp/bankstar.exp >/dev/null 2>/dev/null
rm -r -f /tmp/bankstar >/dev/null 2>/dev/null
rm -r -f /tmp/bankstar.Z >/dev/null 2>/dev/null
[ -f "/tmp/bankdata" -o -f "/tmp/bankdata.Z" ] && {
echo "请先清理 /tmp 然后再备份!n"
read any
return
}
cd /tmp
dbexport bankstar
[ $? -eq 0 ] || {
echo "数据库卸载失败!n"
read any
return
}
tar cvf /tmp/bankstar /usr/bankstar/com /usr/bankstar/data /usr/bankstar/etc /usr/bankstar/frm /tmp/bankstar.exp
chmod 777 /tmp/bankstar >/dev/null 2>/dev/null
compress -H /tmp/bankstar
chmod 777 /tmp/bankstar.Z >/dev/null 2>/dev/null
rm -r -f /tmp/bankstar.exp
[ $? -eq 0 ] || return
cszd=`dbaccess bankstar -<<-End
select val from gcs where dh="005";
End`
gzrq=`echo $cszd | awk 'BEGIN { FS=" " }
{
split($2,a,"/")
rq=sprintf("%s%s%s",a[1],a[2],a[3])
printf rq
}'`
echorq=`echo $cszd | awk 'BEGIN { FS=" " }
{
split($2,a,"/")
rq=sprintf("%s/%s/%s",a[1],a[2],a[3])
printf rq
}'`
echo "正在备份$echorq储蓄数据...."
cd /mnt
change_to_dir bankstar
cp -f /tmp/bankstar.Z "$gzrq".dat
aa=$?
rm -f /tmp/bankstar.Z
[ $aa -eq 0 ] || {
echo "文件(bankstar.Z)拷贝失败!!!"
read any
return
}
echo "[2;7;1m备份数据成功 ! 按回车键返回...[2;7;0mc"
DeleteYqData /mnt/bankstar
read any
echo
}
recover_bankstar()
{
GetDate "所恢复数据的日期"
Date=`echo $date | awk 'BEGIN { FS="/" }{printf("%s%s%s",$1,$2,$3)}'`
BkupFile="$Date.dat"
[ ! -f /mnt/bankstar/$BkupFile ] && {
echo "n 7 7[2;7;4m${date}无备份数据 ! 按回车键返回...[2;7;0mc"
read key
return 1;
}
cd /tmp
rm bankstar.Z 2>/dev/null
cd /mnt/bankstar
[ -f "$Date.dat" ] || { echo "n $Date.dat(数据)文件不存在!!!"
read key
return
}
echo "正在拷贝数据"
cp $Date.dat /tmp/bankstar.Z
[ $? -eq 0 ] || { rm -r /tmp/bankstar.Z>dev/null 2>/dev/null
read key
return
}
cd /tmp
rm -r bankstar 1>/dev/null 2>/dev/null
uncompress bankstar.Z
[ $? -eq 0 ] || { rm -r bankstar.Z>/dev/null 2>/dev/null
read key
return
}
tar xvf bankstar
[ $? -eq 0 ] || { rm -r bankstar.Z >/dev/null 2>/dev/null
rm -r bankstar >/dev/null 2>/dev/null
return
}
dbaccess - -<<-End >/dev/null 2>/dev/null
drop database bankstar;
End
dbimport bankstar -l -i ./
flag=$?
while [ $flag -ne 0 ]
do
GetYN "加载数据库失败 ! 是否重新加载数据库" || return
dbaccess - - <<-End
drop database bankstar;
End
dbimport $DBNAME -l -i ./
flag=$?
done
echo "[2;7;1m恢复成功 ! 按回车键返回...[2;7;0mc"
rm -r -f /tmp/bankstar.exp 2>/dev/null
read key
return
}
#备份对公业务数据
backup_bankacc()
{
cd /tmp
rm -r single.exp 2>/dev/null
rm single.Z 2>/dev/null
dbexport single
flag=$?
while [ $flag -ne 0 ]
do
GetYN " 数据卸载失败 ! 是否重新卸载" || {
rm single.Z >/dev/null 2>/dev/null
echo
return
}
rm -r single.exp 2>/dev/null
dbexport single
flag=$?
done
tar cvf single single.exp
flag=$?
while [ $flag -ne 0 ]
do
GetYN " 数据打包失败 ! 是否重新打包" || {
rm single.Z >/dev/null 2>/dev/null
echo
exit $flag
}
rm single 2>/dev/null
tar cvf single single.exp
flag=$?
done
yes | compress single
flag=$?
while [ $flag -ne 0 ]
do
GetYN " 数据压缩失败 ! 是否重新压缩" || {
rm single.Z >/dev/null 2>/dev/null
echo
return
}
rm single.Z 2>/dev/null
yes | compress single
flag=$?
done
cszd=`dbaccess single -<<-End
select rq from cszd;
End`
gzrq=`echo $cszd | awk 'BEGIN { FS=" " }
{
split($2,a,"/")
rq=sprintf("%s%s%s",a[1],a[2],a[3])
printf rq
}'`
echorq=`echo $cszd | awk 'BEGIN { FS=" " }
{
split($2,a,"/")
rq=sprintf("%s/%s/%s",a[1],a[2],a[3])
printf rq
}'`
echo "正在备份$echorq对公业务数据...."
cd /mnt
change_to_dir single
cp -f /tmp/single.Z "$gzrq".dat
aa=$?
rm -f /tmp/single.Z
[ $aa -eq 0 ] || {
echo "文件(single.Z)拷贝失败!!!"
return
}
echo "[2;7;1m备份数据成功 ! 按回车键返回...[2;7;0mc"
DeleteYqData /mnt/single
read any
echo
}
#恢复对公业务数据
recover_bankacc()
{
GetDate "所恢复数据的日期"
Date=`echo $date | awk 'BEGIN { FS="/" }{printf("%s%s%s",$1,$2,$3)}'`
BkupFile="$Date.dat"
[ ! -f /mnt/single/$BkupFile ] && {
echo "n 7 7[2;7;4m${date}无备份数据 ! 按回车键返回...[2;7;0mc"
read key
return 1;
}
cd /tmp
rm single.Z 2>/dev/null
rm -r single.exp 2>/dev/null
cd /mnt/single
[ -f "$Date.dat" ] || { echo "n $Date.dat(数据)文件不存在!!!"
return
}
echo "正在拷贝数据"
cp $Date.dat /tmp/single.Z
[ $? -eq 0 ] || { rm -r /tmp/single.Z>dev/null 2>/dev/null
return
}
cd /tmp
rm -r single 1>/dev/null 2>/dev/null
uncompress single.Z
[ $? -eq 0 ] || { rm -r single.Z>/dev/null 2>/dev/null
return
}
tar xvf single
[ $? -eq 0 ] || { rm -r single.Z >/dev/null 2>/dev/null
rm -r single >/dev/null 2>/dev/null
return
}
dbaccess - -<<-End >/dev/null 2>/dev/null
drop database single;
End
dbimport single -l -i /tmp
flag=$?
while [ $flag -ne 0 ]
do
GetYN "加载数据库失败 ! 是否重新加载数据库" || return
dbaccess - - <<-End
drop database single;
End
dbimport single -l -i /tmp
flag=$?
done
echo "[2;7;1m恢复成功 ! 按回车键返回...[2;7;0mc"
rm -f /tmp/single.exp 1>/dev/null 2>/dev/null
read key
return
}
##Main Programe
trap '' 2 3
#if [ ! -f "./usu.cfg" ]
#then
# echo "没有配置文件(usu.cfg),请与供应商联系"
# exit
#fi
#. ./usu.cfg
check_privilege #检查权限
mount -o lower ${DEV} ${MOUNT_POINT}
if [ $? -ne 0 ]
then
echo "U盘安装有误 ,请检查!!!!"
sleep 3
exit
fi
while true
do
main_menu
read opt
case $opt in
1)
backup_bankstar
2)
recover_bankstar
3)
backup_bankacc
4)
recover_bankacc
5)
usd_statistics
6)
delete_all
7)
delete_single
8)
list_file
q)
cd /
umount $DEV
[ $? -ne 0 ] && echo "卸载U盘出错"
exit
esac
done
标签: U盘