2015年8月15日土曜日

[bash] dump コマンドでバックアップ


dumpコマンドを使用した、簡単なバックアップ処理です。
/etc/fstab でバックアップ対象に指定したファイルシステムを、別マシンにバックアップします。
リストアの手順は、こちら を参考にしてください。

1.ソースコード


以下は、bash のスクリプトです。マウントした別マシンの領域に、自ホスト名でディレクトリを作成して、そこにバックアップデータを格納します。
バックアップするのは、dumpコマンドで作成したファイルシステムのバックアップと、ディスクやファイルシステム関連の情報です。
スクリプトの主な変数の意味は以下のとおり。
  • $MOUNT_FROM は、バックアップファイルの格納先です。ここでは Windowsの共有フォルダです。
  • $MOUNT_TOは、マウントポイントです。
  • $MOUNT_TYPEは、mountコマンドで指定するタイプです。ここでは Windowsの共有フォルダをマウントするので "cifs" を指定しています。
  • $MOUNT_OPTは、mountコマンドのオプションです。ここでは Windowsの共有フォルダにアクセスするアカウントを指定しています。

#!/bin/sh
#####################################
#
# バックアップ
#
# [参考にしたWEBサイト]
#   http://thinkit.co.jp/cert/compare/5/5/2.htm
#
#####################################

MOUNT_FROM="//192.168.1.2/backup"
MOUNT_TO="/mnt"
MOUNT_TYPE="cifs"
MOUNT_OPT="-o username=ユーザー名,password=パスワード"

BKUP_DIR=${MOUNT_TO}/`uname -n`
DUMP_LEVEL=0

echo "######################################"
echo "# DUMP START `date '+%Y-%m-%d %H:%M:%S'`"
echo "######################################"

if [ "${MOUNT_FROM}" != "" ]; then

    echo "-------------------------------------"
    echo "- mount"
    echo "-------------------------------------"
    mount -t ${MOUNT_TYPE} ${MOUNT_OPT} ${MOUNT_FROM} ${MOUNT_TO}
    if [ $? -ne 0 ]; then
      echo "ERROR: mount failed."
      exit 1
    fi

    df -h

fi

echo "-------------------------------------"
echo "- backup dir : `uname -n`"
echo "-------------------------------------"
if [ ! -d ${BKUP_DIR} ]; then
    echo "- mkdir ${BKUP_DIR}"
    mkdir -p ${BKUP_DIR}
fi
ls -l `dirname ${BKUP_DIR}`

echo "-------------------------------------"
echo "- dump target"
echo "-------------------------------------"
dump -W

echo "-------------------------------------"
echo "- dump full"
echo "-------------------------------------"
sync
dump -W | egrep '^>' | while read line
do

  dev=`echo $line | cut -d' ' -f2`
  bkname=`basename $dev`
  echo "- dev=${dev} bkname=${bkname} level=${DUMP_LEVEL}"
  dump -${DUMP_LEVEL}f - $dev | gzip -c > ${BKUP_DIR}/${bkname}.${DUMP_LEVEL}.dmp.gz

done


echo "-------------------------------------"
echo "- save filesystem info"
echo "-------------------------------------"
fdisk -l       > ${BKUP_DIR}/fdisk.${DUMP_LEVEL}.txt     2>&1
df -k          > ${BKUP_DIR}/df.${DUMP_LEVEL}.txt        2>&1
cat /etc/fstab > ${BKUP_DIR}/fstab.${DUMP_LEVEL}.txt     2>&1
pvdisplay      > ${BKUP_DIR}/pvdisplay.${DUMP_LEVEL}.txt 2>&1
vgdisplay      > ${BKUP_DIR}/vgdisplay.${DUMP_LEVEL}.txt 2>&1
lvdisplay      > ${BKUP_DIR}/lvdisplay.${DUMP_LEVEL}.txt 2>&1

echo "-------------------------------------"
echo "- dump files"
echo "-------------------------------------"
echo "- 容量(Mbyte)"
du -sbm ${BKUP_DIR}
echo "- 一覧"
ls -l ${BKUP_DIR}

if [ "${MOUNT_FROM}" != "" ]; then

    echo "-------------------------------------"
    echo "- umount"
    echo "-------------------------------------"
    umount ${MOUNT_TO}
    if [ $? -ne 0 ]; then
      echo "ERROR: umount failed."
      exit 1
    fi

    df -h
fi

echo "######################################"
echo "# DUMP END `date '+%Y-%m-%d %H:%M:%S'`"
echo "######################################"

exit 0

2.バックアップ処理の実行


上記のスクリプトを実行してみます。

(1)バックアップ先のマシンの用意

今回は、バックアップ先のマシンとしてWindowsXPを用意しました。
WindowsXPに共有フォルダを作成して、そこにバックアップを格納するようにします。

(2)バックアップ処理の実行

まず、/etc/fstab でバックアップ対象を確認しておきます。5番目のフィールドが "1" のファイルシステムがバックアップ対象になります。以下の例では、"/" と "/boot" がバックアップ対象です。
# cat /etc/fstab
/dev/VolGroup00/LogVol00 /                       ext3    defaults        1 1
LABEL=/boot             /boot                   ext3    defaults        1 2
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/VolGroup00/LogVol01 swap                    swap    defaults        0 0 
また、/etc/dumpdates が空ファイルであることを確認します。
# ls -l /etc/dumpdates
-rw-rw-r-- 1 root disk 0 11月 11 13:45 2010 /etc/dumpdates
上記スクリプトは、backup.sh という名前で保存したとします。
rootユーザーで backup.sh を実行すると、実行結果は以下のように表示されます。
# ./backup.sh
######################################
# DUMP START 2011-10-28 16:54:22
######################################
-------------------------------------
- mount
-------------------------------------
Filesystem          サイズ  使用  残り 使用% マウント位置
/dev/mapper/VolGroup00-LogVol00
                      7.1G  1.3G  5.5G  19% /
/dev/hda1              99M   18M   76M  20% /boot
tmpfs                 147M     0  147M   0% /dev/shm
//192.168.1.2/backup  112G   69G   44G  61% /mnt
-------------------------------------
- backup dir : bkserv
-------------------------------------
- mkdir /mnt/bkserv
合計 0
drwxrwxrwx 2 root root 0 11月  4  2011 bkserv
-------------------------------------
- dump target
-------------------------------------
Last dump(s) done (Dump '>' file systems):
> /dev/mapper/VolGroup00-LogVol00       (     /) Last dump: never
> /dev/hda1     ( /boot) Last dump: never
-------------------------------------
- dump full
-------------------------------------
- dev=/dev/mapper/VolGroup00-LogVol00 bkname=VolGroup00-LogVol00 level=0
  DUMP: Date of this level 0 dump: Fri Oct 28 16:54:24 2011
  DUMP: Dumping /dev/mapper/VolGroup00-LogVol00 (/) to standard output
  DUMP: Label: none
  DUMP: Writing 10 Kilobyte records
  DUMP: mapping (Pass I) [regular files]
  DUMP: mapping (Pass II) [directories]
  DUMP: estimated 1138286 blocks.
  DUMP: Volume 1 started with block 1 at: Fri Oct 28 16:55:06 2011
  DUMP: dumping (Pass III) [directories]
  DUMP: dumping (Pass IV) [regular files]
  DUMP: Volume 1 completed at: Fri Oct 28 16:59:49 2011
  DUMP: Volume 1 1325760 blocks (1294.69MB)
  DUMP: Volume 1 took 0:04:43
  DUMP: Volume 1 transfer rate: 4684 kB/s
  DUMP: 1325760 blocks (1294.69MB)
  DUMP: finished in 283 seconds, throughput 4684 kBytes/sec
  DUMP: Date of this level 0 dump: Fri Oct 28 16:54:24 2011
  DUMP: Date this dump completed:  Fri Oct 28 16:59:49 2011
  DUMP: Average transfer rate: 4684 kB/s
  DUMP: DUMP IS DONE
- dev=/dev/hda1 bkname=hda1 level=0
  DUMP: Date of this level 0 dump: Fri Oct 28 16:59:49 2011
  DUMP: Dumping /dev/hda1 (/boot) to standard output
  DUMP: Label: /boot
  DUMP: Writing 10 Kilobyte records
  DUMP: mapping (Pass I) [regular files]
  DUMP: mapping (Pass II) [directories]
  DUMP: estimated 12705 blocks.
  DUMP: Volume 1 started with block 1 at: Fri Oct 28 16:59:50 2011
  DUMP: dumping (Pass III) [directories]
  DUMP: dumping (Pass IV) [regular files]
  DUMP: Volume 1 completed at: Fri Oct 28 16:59:52 2011
  DUMP: Volume 1 12780 blocks (12.48MB)
  DUMP: Volume 1 took 0:00:02
  DUMP: Volume 1 transfer rate: 6390 kB/s
  DUMP: 12780 blocks (12.48MB)
  DUMP: finished in 2 seconds, throughput 6390 kBytes/sec
  DUMP: Date of this level 0 dump: Fri Oct 28 16:59:49 2011
  DUMP: Date this dump completed:  Fri Oct 28 16:59:52 2011
  DUMP: Average transfer rate: 6390 kB/s
  DUMP: DUMP IS DONE
-------------------------------------
- save filesystem info
-------------------------------------
-------------------------------------
- dump files
-------------------------------------
- 容量(Mbyte)
360     /mnt/bkserv
- 一覧
合計 367991
-rwxrwSrwx 1 root root 365635587 11月  4  2011 VolGroup00-LogVol00.0.dmp.gz
-rwxrwSrwx 1 root root       364 11月  4  2011 df.0.txt
-rwxrwSrwx 1 root root       336 11月  4  2011 fdisk.0.txt
-rwxrwSrwx 1 root root       534 11月  4  2011 fstab.0.txt
-rwxrwSrwx 1 root root  11173373 11月  4  2011 hda1.0.dmp.gz
-rwxrwSrwx 1 root root      1041 11月  4  2011 lvdisplay.0.txt
-rwxrwSrwx 1 root root       429 11月  4  2011 pvdisplay.0.txt
-rwxrwSrwx 1 root root       682 11月  4  2011 vgdisplay.0.txt
-------------------------------------
- umount
-------------------------------------
Filesystem          サイズ  使用  残り 使用% マウント位置
/dev/mapper/VolGroup00-LogVol00
                      7.1G  1.3G  5.5G  19% /
/dev/hda1              99M   18M   76M  20% /boot
tmpfs                 147M     0  147M   0% /dev/shm
######################################
# DUMP END 2011-10-28 16:59:55
######################################

Windowsの共有フォルダにあるバックアップファイルを参照したい場合は、以下のようにマウントします。
# mount -t cifs -o username=ユーザー名,password=パスワード //192.168.1.2/backup /mnt