Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- trap '{ stty sane; echo ""; errexit "Aborted"; }' SIGINT SIGTERM
- HOST="$(hostname)"
- ADDBLK=0
- BOOTBEG=2048
- BOOTEND=88063
- ROOTBEG=88064
- MNTPATH="/root/img-backup-mnt"
- # change to the path of the network share where the backup file will be stored
- NSHARE="/YOUR/PATH/"
- ONEMB=$((1024 * 1024))
- mkloop1()
- {
- local INFO1=""
- local SIZE1=0
- INFO1="$(fdisk -lu "${IMGFILE}")"
- START1=$(grep W95 <<< "${INFO1}" | awk '{print $2}')
- SIZE1=$(grep W95 <<< "${INFO1}" | awk '{print $4}')
- LOOP1="$(losetup -f --show -o $((${START1} * 512)) --sizelimit $((${SIZE1} * 512)) "${IMGFILE}")"
- if [ $? -ne 0 ]; then
- errexit "$HOST - Unable to create BOOT loop device"
- fi
- }
- rmloop1()
- {
- if [ "${LOOP1}" != "" ]; then
- losetup -d "${LOOP1}"
- LOOP1=""
- fi
- }
- mkloop2()
- {
- local INFO2=""
- local SIZE2=0
- INFO2="$(fdisk -lu "${IMGFILE}")"
- START2=$(grep Linux <<< "${INFO2}" | awk '{print $2}')
- SIZE2=$(grep Linux <<< "${INFO2}" | awk '{print $4}')
- LOOP2="$(losetup -f --show -o $((${START2} * 512)) --sizelimit $((${SIZE2} * 512)) "${IMGFILE}")"
- if [ $? -ne 0 ]; then
- errexit "$HOST - Unable to create ROOT loop device"
- fi
- }
- rmloop2()
- {
- if [ "${LOOP2}" != "" ]; then
- losetup -d "${LOOP2}"
- LOOP2=""
- fi
- }
- mntimg()
- {
- if [ ! -d "${MNTPATH}"/ ]; then
- mkdir "${MNTPATH}"/
- if [ $? -ne 0 ]; then
- errexit "$HOST - Unable to make ROOT partition mount point"
- fi
- fi
- mkloop2
- mount "${LOOP2}" "${MNTPATH}"/
- if [ $? -ne 0 ]; then
- errexit "$HOST - Unable to mount image ROOT partition"
- fi
- if [ ! -d "${MNTPATH}"/boot/ ]; then
- mkdir -p "${MNTPATH}"/boot/
- if [ $? -ne 0 ]; then
- errexit "$HOST - Unable to make BOOT partition mount point"
- fi
- fi
- mkloop1
- mount "${LOOP1}" "${MNTPATH}"/boot/
- if [ $? -ne 0 ]; then
- errexit "$HOST - Unable to mount image BOOT partition"
- fi
- }
- umntimg()
- {
- umount "${MNTPATH}"/boot/
- if [ $? -ne 0 ]; then
- errexit "$HOST - Unable to unmount image BOOT partition"
- fi
- rmloop1
- umount "${MNTPATH}"/
- if [ $? -ne 0 ]; then
- errexit "$HOST - Unable to unmount image ROOT partition"
- fi
- rmloop2
- rm -r "${MNTPATH}"/
- }
- errexit()
- {
- (pushover backup "$1" &)
- echo ""
- echo "$1"
- echo ""
- umount "${MNTPATH}"/boot/ &> /dev/null
- umount "${MNTPATH}"/ &> /dev/null
- rm -r "${MNTPATH}"/ &> /dev/null
- rmloop1
- rmloop2
- exit 1
- }
- LOOP1=""
- LOOP2=""
- PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
- if [ $(id -u) -ne 0 ]; then
- errexit "$HOST - $0 must be run as root user"
- fi
- rsync --version &> /dev/null
- if [ $? -ne 0 ]; then
- errexit "$HOST - rsync not installed (run: apt-get install rsync)"
- fi
- if command -v systemctl > /dev/null && systemctl | grep -q '\-\.mount'; then
- SYSTEMD=1
- elif [ -f /etc/init.d/cron ] && [ ! -h /etc/init.d/cron ]; then
- SYSTEMD=0
- else
- errexit "$HOST - Unrecognized init system"
- fi
- if [ ${SYSTEMD} -eq 1 ]; then
- ROOT_PART="$(mount | sed -n 's|^/dev/\(.*\) on / .*|\1|p')"
- else
- if [ ! -h /dev/root ]; then
- errexit "$HOST - /dev/root does not exist or is not a symlink"
- fi
- ROOT_PART="$(readlink /dev/root)"
- fi
- ROOT_DEV="${ROOT_PART:0:(${#ROOT_PART} - 1)}"
- if [ "${ROOT_DEV}" = "mmcblk0p" ]; then
- ROOT_DEV="${ROOT_DEV:0:(${#ROOT_DEV} - 1)}"
- fi
- PTUUID="$(blkid /dev/"${ROOT_DEV}" | sed -n 's|^.*PTUUID=\"\(\S\+\)\".*|\1|p')"
- INFO="$(tune2fs -l /dev/"${ROOT_PART}" 2> /dev/null)"
- BLKCNT=$(grep '^Block count:' <<< "${INFO}" | awk '{print $3}')
- BLKSIZE=$(grep '^Block size:' <<< "${INFO}" | awk '{print $3}')
- INFO="$(df | grep /dev/root)"
- DFKSIZE=$(awk '{print $2}' <<< "${INFO}")
- DFKFREE=$(awk '{print $4}' <<< "${INFO}")
- ROOTSIZE=$((${BLKCNT} * ${BLKSIZE}))
- ROOTUSED=$(((${DFKSIZE} - ${DFKFREE}) * 1024))
- IRFSMIN=$(((${ROOTUSED} + (${ADDBLK} * ${BLKSIZE}) + (${ONEMB} - 1)) / ${ONEMB}))
- IRFSMAX=$(((${ROOTSIZE} + (${ONEMB} - 1)) / ${ONEMB}))
- IMGFILE="$1"
- HOST="$(hostname)"
- MON="$(date +%B)"
- YR="$(date +%Y)"
- if [ "${IMGFILE}" = "" ]; then
- while :
- do
- echo ""
- read -r -e -i "${IMGFILE}" -p "Image file to create? ( $NSHARE/BackupImage-$HOST$MON$YR )" IMGFILE
- if [ "${IMGFILE}" = "" ]; then
- continue
- elif [ -d "${IMGFILE}" ]; then
- echo ""
- echo "${IMGFILE} is a directory"
- elif [ -f "${IMGFILE}" ]; then
- echo ""
- echo -n "${IMGFILE} already exists, Ok to delete (y/n)? "
- while read -r -n 1 -s answer; do
- if [[ "${answer}" = [yYnN] ]]; then
- echo "${answer}"
- if [[ "${answer}" = [yY] ]]; then
- break 2
- else
- break 1
- fi
- fi
- done
- else
- break
- fi
- done
- IRFSSIZE=""
- while :
- do
- echo ""
- read -r -e -i "${IRFSSIZE}" -p "Image ROOT filesystem size (MB) [${IRFSMAX}]? " IRFSSIZE
- if [ "${IRFSSIZE}" = "" ]; then
- IRFSSIZE=${IRFSMAX}
- break
- elif [ ${IRFSSIZE} -ge ${IRFSMIN} ]; then
- break
- else
- echo ""
- echo "Requested image ROOT filesystem size (${IRFSSIZE}) is too small (Minimum = ${IRFSMIN})"
- IRFSSIZE=${IRFSMIN}
- fi
- done
- echo ""
- echo -n "Create ${IMGFILE} [${IRFSSIZE} MB] (y/n)? "
- while read -r -n 1 -s answer; do
- if [[ "${answer}" = [yYnN] ]]; then
- echo "${answer}"
- if [[ "${answer}" = [yY] ]]; then
- break
- else
- errexit "$HOST - Aborted"
- fi
- fi
- done
- if [ -f "${IMGFILE}" ]; then
- rm "${IMGFILE}"
- if [ $? -ne 0 ]; then
- errexit "$HOST - Unable to delete existing image file"
- fi
- fi
- ROOTEND=$((${ROOTBEG} + ((${IRFSSIZE} * ${ONEMB}) / 512) - 1))
- truncate -s $(((${ROOTEND} + 1) * 512)) "${IMGFILE}"
- if [ $? -ne 0 ]; then
- errexit "$HOST - Unable to create image file"
- fi
- fdisk "${IMGFILE}" <<EOF > /dev/null
- p
- n
- p
- 1
- ${BOOTBEG}
- ${BOOTEND}
- t
- c
- p
- n
- p
- 2
- ${ROOTBEG}
- ${ROOTEND}
- p
- w
- EOF
- mkloop1
- mkfs.vfat "${LOOP1}" > /dev/null
- if [ $? -ne 0 ]; then
- errexit "$HOST - Unable to create image BOOT filesystem (vfat)"
- fi
- dosfsck "${LOOP1}" > /dev/null
- if [ $? -ne 0 ]; then
- errexit "$HOST - Image BOOT filesystem appears corrupted"
- fi
- rmloop1
- mkloop2
- mkfs.ext4 -q -b ${BLKSIZE} "${LOOP2}" > /dev/null
- if [ $? -ne 0 ]; then
- errexit "$HOST - Unable to create image ROOT filesystem (ext4)"
- fi
- rmloop2
- fdisk "${IMGFILE}" <<EOF > /dev/null
- p
- x
- i
- 0x${PTUUID}
- r
- p
- w
- EOF
- mntimg
- mkdir "${MNTPATH}"/dev/ "${MNTPATH}"/media/ "${MNTPATH}"/mnt/ "${MNTPATH}"/proc/ "${MNTPATH}"/run/ "${MNTPATH}"/sys/ "${MNTPATH}"/tmp/
- if [ $? -ne 0 ]; then
- errexit "$HOST - Unable to create image directories"
- fi
- chmod a+rwxt "${MNTPATH}"/tmp/
- umntimg
- echo ""
- echo "Starting $HOST full backup. For incremental backups, run: $0 ${IMGFILE}"
- elif [ -d "${IMGFILE}" ]; then
- errexit "$HOST - ${IMGFILE} is a directory"
- elif [ ! -f "${IMGFILE}" ]; then
- errexit "$HOST - ${IMGFILE} not found"
- fi
- mntimg
- sync
- rsync -aDH --partial --numeric-ids --delete --force --exclude "${MNTPATH}" --exclude '/dev' --exclude '/media' --exclude '/mnt' --exclude '/proc' --exclude '/run' --exclude '/home/pi/Pictures' --exclude '/sys' \
- --exclude '/tmp' --exclude 'lost\+found' --exclude '/etc/udev/rules.d/70-persistent-net.rules' --exclude '/var/lib/asterisk/astdb.sqlite3-journal' / "${MNTPATH}"/
- if [ $? -ne 0 ]; then
- errexit "$HOST - Unable to create backup"
- fi
- sync
- umntimg
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement