每次用dd备份整个磁盘的时候都很方便,偶然听到朋友问我位置参数的问题,于是心血来潮,干脆写个shell脚本实现自动备份,以后就不用找源磁盘,目的磁盘,挂载分区等等操作了,代码帮我实现。
以下是我花了连续5个小时调试好的脚本,虽然有些拙劣,但是也是小小的成功吧,每天进步一点点。
#!/bin/bash
# Author: Attaboy
# Description: backup whole disk of S5 and HSTONG office PC's SSD using dd
# Modify history:
# 2018-06-23 first release
# 2018-06-24 modify
# 2018-06-25 modify
log="./dd_backup_disk.log"
mount_dir="./m_dir"
S5_bak_src_dev=$(fdisk -l 2>/dev/null | awk -F '[ \t]+|:' '/111.+G/{print $2}')
HSTONG_bak_src_dev=$(fdisk -l 2>/dev/null | awk -F '[ \t]+|:' '/111.+G/{print $2}')
S5_bak_dst_dev=$(fdisk -l 2>/dev/null | awk -F '[ \t]+|:' '/1.8.+T/{print $2}')"5"
HSTONG_bak_dst_dev=$(fdisk -l 2>/dev/null | awk -F '[ \t]+|:' '/931\.5.+GB/{print $2}')"1"
S5_bak_dst_file="${mount_dir}/backup/home_dev_bak/s5_os_bak/ssd_bak_$(date +%Y%m%d).bz2"
HSTONG_bak_dst_file="${mount_dir}/Hstong-ssd-backup-$(date +%Y%m%d).bz2"
echo ${S5_bak_src_dev}
echo ${S5_bak_dst_dev}
echo ${S5_bak_dst_file}
echo ${HSTONG_bak_src_dev}
echo ${HSTONG_bak_dst_dev}
echo ${HSTONG_bak_dst_file}
usage() {
echo -e "\nSyntax Error! Only one argument is allowed!" >>${log} 2>&1
echo -e "Usage: script.sh [S5|HSTONG]\n" >>${log} 2>&1
exit 7
}
pre_chk() {
# check current execute user.
if [ $UID -ne "0" ] ; then
echo -e "\n...... Current user is not root." >>${log} 2>&1
exit -1
fi
echo -e "\n====================================================\n" >>${log} 2>&1
echo -e "Check passed!!" >>${log} 2>&1
echo -e "\n====================================================\n" >>${log} 2>&1
}
backup() {
# check the number of arguments
if [ $# -ne "3" ] ; then
echo -e "\n3 arguments will be specified.\n"
echo -e "\nUsage: backup [argu1] [argu2] [argu3]\n"
exit 10
fi
# check mount dir exists
if [ ! -d ${mount_dir} ] ; then
mkdir -p ${mount_dir}
fi
# mount dst device
\df -hT | grep -iE "$2"
if [ $? -eq "0" ] ; then
umount -l $2 >>${log} 2>&1
mount -t ntfs-3g $2 ${mount_dir} >>${log} 2>&1
elif [ $? -ne "0" ] ; then
mount -t ntfs-3g $2 ${mount_dir} >>${log} 2>&1
else
echo -e "\nOther mount problem!!!\n" >>${log} 2>&1
fi
\df -hT | grep -iE "$2"
# check mount
if [ $? -eq "0" ] ; then
echo -e "\n>>> Mount dst backup device OK\n" >>${log} 2>&1
\df -hT >>${log} 2>&1
else
echo -e "\nMount dst backup device FAILED\n" >>${log} 2>&1
exit 8
fi
# check disk space
_dst_space=$(\df -hT | grep -iE "$2" | awk -F '[ \t]+|G' '{print $5}')
if [ ${_dst_space} -le "50" ] ; then
echo -e "\nDisk space is less than 50GB\n" >>${log} 2>&1
exit 9
else
echo -e "\nDisk space is adequate\n" >>${log} 2>&1
fi
# start to backup
echo -e "\nStarting backup ...... \n" >>${log} 2>&1
dd if=$1 | bzip2 >$3
}
post_do(){
# check the number of arguments
if [ $# -ne "1" ] ; then
echo -e "Syntax Error, Only one argument is allowed!" >>${log} 2>&1
exit -2
fi
# check if dst backup file exists
bak_file_size=$(du -m $1 | awk '{print $1}')
if [ ! -s $1 ] ; then
echo -e "\nBackup file not exists!!\n" >>${log} 2>&1
exit 9
elif [ ${bak_file_size} -le "20000" ] ; then
echo -e "Dst backup file size is less than 20000 MB !!" >>${log} 2>&1
exit -3
else
echo -e "Congratulations, backup succeeded!!" >>${log} 2>&1
fi
ping_num="4"
echo -e "\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n" >>${log} 2>&1
echo -e "Your machine will halt in ${ping_num} seconds." >>${log} 2>&1
echo -e "\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n" >>${log} 2>&1
ping -c ${ping_num} 127.0.0.1 >>${log} 2>&1
# shutdown
init 0
}
# start to backup
if [ $# -gt "1" ] ; then
usage
elif [ $# -lt "1" ] ; then
echo -e "\nNo arguments found.\n" >>${log} 2>&1
exit 7
else
pre_chk
fi
case $1 in
S5|s5)
if [ -z ${S5_bak_src_dev} ] ; then
echo -e "\nSrc backup device not exists!!\n" >>${log} 2>&1
exit 6
else
backup ${S5_bak_src_dev} ${S5_bak_dst_dev} ${S5_bak_dst_file}
post_do ${S5_bak_dst_file}
fi
;;
HSTONG|hs|HS|hstong|HST)
if [ -z ${HSTONG_bak_src_dev} ] ; then
echo -e "\nSrc backup device not exists!!\n" >>${log} 2>&1
exit 6
else
backup ${HSTONG_bak_src_dev} ${HSTONG_bak_dst_dev} ${HSTONG_bak_dst_file}
post_do ${HSTONG_bak_dst_file}
fi
;;
*)
usage
;;
esac
exit 0