自己写的一个在新硬盘上自动恢复系统备份的脚本

类别:软件工程 点击:0 评论:0 推荐:

说明:
之前开发了一个基于Freebsd的准嵌入式系统。基本系统放在DOM上,用户数据放在硬盘上。因为系统比较复杂,每次恢复/安装系统都很繁琐。就自己写了一个脚本。实现了自动安装。
其中包括自动对硬盘进行分区,自动格式化等功能。有兴趣的朋友可以看看:)
欢迎指点!:)

#!/bin/sh

#Root Directory
BASE_DIR=/root/autobuild/backup-pack
Backup_Dir="$BASE_DIR"/NetEngine/System-Backup

#Partition directory
USR_DIR=/mnt/usr
VAR_DIR=/mnt/var
TMP_DIR=/mnt/tmp
HOME_DIR=/mnt/home

#Device
DISKLABEL=/sbin/disklabel
DEVICEDIR=dev
HARDDISK=ad1
DOM=ad2
LABEL=s1
PARTITIONFILE=/tmp/partition
USRNUM=d
VARNUM=e
TMPNUM=f
HOMENUM=g
DOM_DEVICE=/"$DEVICEDIR"/"$DOM"

# Auto Fdisk
#Get harddisk memory
disksize=`$DISKLABEL /"$DEVICEDIR"/"$HARDDISK" | grep "sectors/unit" | awk '{print $2}'`

disksize=`expr $disksize - 409600`
 
#Get usr spec
usrsize=`expr $disksize / 10 \* 5`

#Get var memory
varsize=`expr $disksize / 10 \* 2`

#Get tmp memory
tmpsize=`expr $disksize / 10 \* 1 - 20000`
tmpoffset=`expr $usrsize + $varsize`

#Get home memory
homesize=`expr $disksize / 10 \* 2`
homeoffset=`expr $tmpoffset + $tmpsize`

#Get swap offset
swapoffset=`expr $homeoffset + $homesize`

#Confirm tmp memory

#Initial harddisk
echo -n "Init harddisk partition...    "
dd>/dev/null 2>&1 if=/dev/zero of=/"$DEVICEDIR"/"$HARDDISK" bs=1k count=1
fdisk>/dev/null 2>&1 -BI /"$DEVICEDIR"/"$HARDDISK"
$DISKLABEL>/dev/null 2>&1 -B -w -r $HARDDISK$LABEL auto
$DISKLABEL >$PARTITIONFILE /"$DEVICEDIR"/"$HARDDISK"$LABEL

#Initial partition table
echo "d:    $usrsize    0             4.2BSD   2048   16384   89">>$PARTITIONFILE
echo "e:    $varsize    $usrsize      4.2BSD   2048   16384   89">>$PARTITIONFILE
echo "f:    $tmpsize    $tmpoffset   4.2BSD   2048   16384   89">>$PARTITIONFILE
echo "g:    $homesize   $homeoffset    4.2BSD   2048   16384   89">>$PARTITIONFILE
echo "h:    409600      $swapoffset   swap">>$PARTITIONFILE

#Auto partition
$DISKLABEL>/dev/null 2>&1 -R /"$DEVICEDIR"/"$HARDDISK"$LABEL $PARTITIONFILE
echo "done"

#Create file system
echo -n "Format harddisk...            "
newfs>/dev/null 2>&1 /"$DEVICEDIR"/"$HARDDISK"$LABEL$USRNUM
newfs>/dev/null 2>&1 /"$DEVICEDIR"/"$HARDDISK"$LABEL$VARNUM
newfs>/dev/null 2>&1 /"$DEVICEDIR"/"$HARDDISK"$LABEL$HOMENUM
newfs>/dev/null 2>&1 /"$DEVICEDIR"/"$HARDDISK"$LABEL$TMPNUM
echo "done"

# Mount all harddisk partition
echo -n "Mount all partition...        "
mount /"$DEVICEDIR"/"$HARDDISK"$LABEL$USRNUM $USR_DIR
mount /"$DEVICEDIR"/"$HARDDISK"$LABEL$VARNUM $VAR_DIR
mount /"$DEVICEDIR"/"$HARDDISK"$LABEL$HOMENUM $HOME_DIR
mount /"$DEVICEDIR"/"$HARDDISK"$LABEL$TMPNUM $TMP_DIR
echo "done"

#Auto burn dom image
echo -n "Burn dom image...             "
dd>/dev/null 2>&1 if=$Backup_Dir/NetEngine-dombak.img of=$DOM_DEVICE
echo "done"

#Auto uncompress tar.gz pack to harddisk
cd $USR_DIR
echo -n "Unpack usr directory...       "
tar>/dev/null 2>&1 zxvf $Backup_Dir/usrbak.tar.gz ./
echo "done"

cd $VAR_DIR
echo -n "Unpack var directory...       "
tar>/dev/null 2>&1 zxvf $Backup_Dir/varbak.tar.gz ./
echo "done"

cd $TMP_DIR
echo -n "Unpack tmp directory...       "
tar>/dev/null 2>&1 zxvf $Backup_Dir/tmpbak.tar.gz ./
echo "done"

cd $HOME_DIR
echo -n "Unpack home directory...      "
tar>/dev/null 2>&1 zxvf $Backup_Dir/homebak.tar.gz ./
echo "done"

# Umount harddisk
cd /
echo -n "Unmount all partition...      "
umount $USR_DIR $VAR_DIR $TMP_DIR $HOME_DIR
echo "done"

本文地址:http://com.8s8s.com/it/it33759.htm