2009年9月29日 星期二

XFS Backup/Restore

 XFS Backup/Restore

轉載

一般 unix base 的備份多使用 tar, cpio, dump 之類的軟體. 最常看見的應該算是 tar 了. 不過 tar 目前只能備份標準的一些檔案屬性, 對於 XFS 來說,額外的 ACL 屬性就沒有辦法了. 所以如果使用 XFS, 通常就會利用本身所提供的工具來備份.

下面的 script 就是利用 xfsdump, bzip2, dd 備份資料到磁帶上. 備份後會測試一次, 以確定資料完整.

#!/bin/sh

# 磁帶機的裝置名稱
TAPE_DEV="/dev/nst0"

# null 裝置名稱
NULL_DEV="/dev/null"

# 指令檔存放的路徑
CMD_PATH="/etc/backup"

# 磁帶的區塊大小 (TR-5 使用 9-65536, 備份與還原必須使用相同設定)
# 配合壓縮軟體與 dd 時, 若設定過大會有問題
BLOCK_SIZE=1024

# 備份的等級, 0 為完全備份
BACKUP_LEVEL=0

# 備份時顯示的間格秒數
TIME_DISPLAY=300

# 備份的指令檔
DUMP_CMD="/usr/sbin/xfsdump"

# 還原備份的指令檔
RESTORE_CMD="/usr/sbin/xfsrestore"

# mt 指令
MT_CMD="/bin/mt"

# dd 指令
DD_CMD="/bin/dd"

# 壓縮程式
ZIP_CMD="/usr/bin/bzip2"

# ls 指令
LS_CMD="/bin/ls"

# head 指令
HEAD_CMD="/usr/bin/head"

# 備份路徑設定檔
# 檔案格式如下:
#
#/dev/hda1 /
#/dev/vg01/ora /home/oracle oracle_stop oracle_start
VOLS_INCL_FILE="${CMD_PATH}/vols.include.xfsdump"

if [ -f ${VOLS_INCL_FILE} ]; then
date
echo "---------------------------------------------------------------"
echo "如果要還原備份資料, 可以使用下面的指令:"
echo "${DD_CMD} bs=${BLOCK_SIZE} conv=sync if=${TAPE_DEV} | \ "
echo " ${ZIP_CMD} -dqv | \ "
echo " ${RESTORE_CMD} -p ${TIME_DISPLAY} -J - /path/to/restore/"
echo "---------------------------------------------------------------"
echo

echo "---------------------------------------------------------------"
echo "列出本檔案的內容 - ${0}"
echo "---------------------------------------------------------------"
cat ${0}
echo "---------------------------------------------------------------"
echo "檔案結束!"
echo "---------------------------------------------------------------"
echo

echo "---------------------------------------------------------------"
echo "將磁帶回轉, 並設定區塊大小"
echo "cmd: ${MT_CMD} -f ${TAPE_DEV} retension"
echo "cmd: ${MT_CMD} -f ${TAPE_DEV} rewind"
echo "cmd: ${MT_CMD} -f ${TAPE_DEV} setblk ${BLOCK_SIZE}"
echo "---------------------------------------------------------------"
echo
${MT_CMD} -f ${TAPE_DEV} retension
${MT_CMD} -f ${TAPE_DEV} rewind
${MT_CMD} -f ${TAPE_DEV} setblk ${BLOCK_SIZE}

echo "---------------------------------------------------------------"
echo "載入設定檔: ${VOLS_INCL_FILE}"
index=0
declare -a device_names
declare -a session_names
declare -a before_cmds
declare -a after_cmds
exec 9<&0
exec <${VOLS_INCL_FILE}
while read DEVICE_NAME SESSION_NAME BEFORE_CMD AFTER_CMD
do
case "${DEVICE_NAME}" in
""|\#*)
continue;
;;
esac
device_names[$index]=${DEVICE_NAME}
session_names[$index]=${SESSION_NAME}
before_cmds[$index]=${BEFORE_CMD}
after_cmds[$index]=${AFTER_CMD}
let "index += 1"
done
echo "共有 ${index} 個檔案系統要備份!"
echo "---------------------------------------------------------------"
exec 0<&9 9<&-

num=0
while [ "$num" -lt "$index" ]; do
DEVICE_NAME=${device_names[$num]}
SESSION_NAME=${session_names[$num]}
BEFORE_CMD=${before_cmds[$num]}
AFTER_CMD=${after_cmds[$num]}
let "num += 1"

date
echo "---------------------------------------------------------------"
echo "備份 ${DEVICE_NAME} 到磁帶 ${TAPE_DEV} 中..."
echo "---------------------------------------------------------------"
if [ "${BEFORE_CMD}" != "" ]; then
echo " *** 執行備份前置指令: ${CMD_PATH}/${BEFORE_CMD}"
${CMD_PATH}/${BEFORE_CMD}
fi
echo " *** 開始備份..."
echo "cmd: ${DUMP_CMD} \
-l ${BACKUP_LEVEL} \
-o \
-p ${TIME_DISPLAY} \
-J \
-F \
-M weekly_backup \
-L ${SESSION_NAME} \
- ${DEVICE_NAME} | \
${ZIP_CMD} -9qv | \
${DD_CMD} bs=${BLOCK_SIZE} conv=sync of=${TAPE_DEV}"
${DUMP_CMD} \
-l ${BACKUP_LEVEL} \
-o \
-p ${TIME_DISPLAY} \
-J \
-F \
-M weekly_backup \
-L ${SESSION_NAME} \
- ${DEVICE_NAME} | \
${ZIP_CMD} -9qv | \
${DD_CMD} bs=${BLOCK_SIZE} conv=sync of=${TAPE_DEV}
echo " *** 備份完成!"
if [ "${AFTER_CMD}" != "" ]; then
echo " *** 執行備份後置指令: ${CMD_PATH}/${AFTER_CMD}"
${CMD_PATH}/${AFTER_CMD}
fi
done

date
echo "---------------------------------------------------------------"
echo "備份完成!"
echo "---------------------------------------------------------------"

echo "---------------------------------------------------------------"
echo "將磁帶回轉"
echo "cmd: ${MT_CMD} -f ${TAPE_DEV} rewind"
echo "---------------------------------------------------------------"
echo
${MT_CMD} -f ${TAPE_DEV} rewind

num=0
lnum=0
while [ "$num" -lt "$index" ]; do
DEVICE_NAME=${device_names[$num]}
SESSION_NAME=${session_names[$num]}
BEFORE_CMD=${before_cmds[$num]}
AFTER_CMD=${after_cmds[$num]}
let "num += 1"

date
echo "---------------------------------------------------------------"
echo "測試備份內容 ${DEVICE_NAME}"
echo "---------------------------------------------------------------"
echo "cmd: ${DD_CMD} bs=${BLOCK_SIZE} conv=sync if=${TAPE_DEV} | ${ZIP_CMD} -tqv"
${DD_CMD} bs=${BLOCK_SIZE} conv=sync if=${TAPE_DEV} | ${ZIP_CMD} -tqv
echo
echo "---------------------------------------------------------------"
echo "如果要還原備份資料, 可以使用下面的指令:"
echo "${MT_CMD} -f ${TAPE_DEV} rewind"
echo "${MT_CMD} -f ${TAPE_DEV} fsf ${lnum}"
echo "${DD_CMD} bs=${BLOCK_SIZE} conv=sync if=${TAPE_DEV} | \ "
echo " ${ZIP_CMD} -dqv | \ "
echo " ${RESTORE_CMD} -p ${TIME_DISPLAY} -J - /path/to/restore/"
echo "---------------------------------------------------------------"
echo
let "lnum += 1"
done
fi

date
echo "---------------------------------------------------------------"
echo "將磁帶回轉"
echo "cmd: ${MT_CMD} -f ${TAPE_DEV} rewind"
echo "---------------------------------------------------------------"
echo
${MT_CMD} -f ${TAPE_DEV} rewind

date
exit 0

本文的最新版本可以由下列的網頁取得:

http://www.teatime.com.tw/~tommy/doc/xfs_backup.txt

沒有留言:

張貼留言