Advertisement
RamSet

imgresize

Jul 9th, 2020 (edited)
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.79 KB | None | 0 0
  1. #!/bin/bash
  2. trap '{ stty sane; echo ""; errexit "Aborted"; }' SIGINT SIGTERM
  3. HOST="$(hostname)"
  4. ADDBLK=0
  5. BOOTBEG=2048
  6. BOOTEND=88063
  7. ROOTBEG=88064
  8. MNTPATH="/root/img-backup-mnt"
  9. # change to the path of the network share where the backup file will be stored
  10. NSHARE="/YOUR/PATH/"
  11. ONEMB=$((1024 * 1024))
  12. mkloop1()
  13. {
  14. local INFO1=""
  15. local SIZE1=0
  16. INFO1="$(fdisk -lu "${IMGFILE}")"
  17. START1=$(grep W95 <<< "${INFO1}" | awk '{print $2}')
  18. SIZE1=$(grep W95 <<< "${INFO1}" | awk '{print $4}')
  19. LOOP1="$(losetup -f --show -o $((${START1} * 512)) --sizelimit $((${SIZE1} * 512)) "${IMGFILE}")"
  20. if [ $? -ne 0 ]; then
  21. errexit "$HOST - Unable to create BOOT loop device"
  22. fi
  23. }
  24. rmloop1()
  25. {
  26. if [ "${LOOP1}" != "" ]; then
  27. losetup -d "${LOOP1}"
  28. LOOP1=""
  29. fi
  30. }
  31. mkloop2()
  32. {
  33. local INFO2=""
  34. local SIZE2=0
  35. INFO2="$(fdisk -lu "${IMGFILE}")"
  36. START2=$(grep Linux <<< "${INFO2}" | awk '{print $2}')
  37. SIZE2=$(grep Linux <<< "${INFO2}" | awk '{print $4}')
  38. LOOP2="$(losetup -f --show -o $((${START2} * 512)) --sizelimit $((${SIZE2} * 512)) "${IMGFILE}")"
  39. if [ $? -ne 0 ]; then
  40. errexit "$HOST - Unable to create ROOT loop device"
  41. fi
  42. }
  43. rmloop2()
  44. {
  45. if [ "${LOOP2}" != "" ]; then
  46. losetup -d "${LOOP2}"
  47. LOOP2=""
  48. fi
  49. }
  50. mntimg()
  51. {
  52. if [ ! -d "${MNTPATH}"/ ]; then
  53. mkdir "${MNTPATH}"/
  54. if [ $? -ne 0 ]; then
  55. errexit "$HOST - Unable to make ROOT partition mount point"
  56. fi
  57. fi
  58. mkloop2
  59. mount "${LOOP2}" "${MNTPATH}"/
  60. if [ $? -ne 0 ]; then
  61. errexit "$HOST - Unable to mount image ROOT partition"
  62. fi
  63. if [ ! -d "${MNTPATH}"/boot/ ]; then
  64. mkdir -p "${MNTPATH}"/boot/
  65. if [ $? -ne 0 ]; then
  66. errexit "$HOST - Unable to make BOOT partition mount point"
  67. fi
  68. fi
  69. mkloop1
  70. mount "${LOOP1}" "${MNTPATH}"/boot/
  71. if [ $? -ne 0 ]; then
  72. errexit "$HOST - Unable to mount image BOOT partition"
  73. fi
  74. }
  75. umntimg()
  76. {
  77. umount "${MNTPATH}"/boot/
  78. if [ $? -ne 0 ]; then
  79. errexit "$HOST - Unable to unmount image BOOT partition"
  80. fi
  81. rmloop1
  82. umount "${MNTPATH}"/
  83. if [ $? -ne 0 ]; then
  84. errexit "$HOST - Unable to unmount image ROOT partition"
  85. fi
  86. rmloop2
  87. rm -r "${MNTPATH}"/
  88. }
  89. errexit()
  90. {
  91. (pushover backup "$1" &)
  92. echo ""
  93. echo "$1"
  94. echo ""
  95. umount "${MNTPATH}"/boot/ &> /dev/null
  96. umount "${MNTPATH}"/ &> /dev/null
  97. rm -r "${MNTPATH}"/ &> /dev/null
  98. rmloop1
  99. rmloop2
  100. exit 1
  101. }
  102. LOOP1=""
  103. LOOP2=""
  104. PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  105. if [ $(id -u) -ne 0 ]; then
  106. errexit "$HOST - $0 must be run as root user"
  107. fi
  108. rsync --version &> /dev/null
  109. if [ $? -ne 0 ]; then
  110. errexit "$HOST - rsync not installed (run: apt-get install rsync)"
  111. fi
  112. if command -v systemctl > /dev/null && systemctl | grep -q '\-\.mount'; then
  113. SYSTEMD=1
  114. elif [ -f /etc/init.d/cron ] && [ ! -h /etc/init.d/cron ]; then
  115. SYSTEMD=0
  116. else
  117. errexit "$HOST - Unrecognized init system"
  118. fi
  119. if [ ${SYSTEMD} -eq 1 ]; then
  120. ROOT_PART="$(mount | sed -n 's|^/dev/\(.*\) on / .*|\1|p')"
  121. else
  122. if [ ! -h /dev/root ]; then
  123. errexit "$HOST - /dev/root does not exist or is not a symlink"
  124. fi
  125. ROOT_PART="$(readlink /dev/root)"
  126. fi
  127. ROOT_DEV="${ROOT_PART:0:(${#ROOT_PART} - 1)}"
  128. if [ "${ROOT_DEV}" = "mmcblk0p" ]; then
  129. ROOT_DEV="${ROOT_DEV:0:(${#ROOT_DEV} - 1)}"
  130. fi
  131. PTUUID="$(blkid /dev/"${ROOT_DEV}" | sed -n 's|^.*PTUUID=\"\(\S\+\)\".*|\1|p')"
  132. INFO="$(tune2fs -l /dev/"${ROOT_PART}" 2> /dev/null)"
  133. BLKCNT=$(grep '^Block count:' <<< "${INFO}" | awk '{print $3}')
  134. BLKSIZE=$(grep '^Block size:' <<< "${INFO}" | awk '{print $3}')
  135. INFO="$(df | grep /dev/root)"
  136. DFKSIZE=$(awk '{print $2}' <<< "${INFO}")
  137. DFKFREE=$(awk '{print $4}' <<< "${INFO}")
  138. ROOTSIZE=$((${BLKCNT} * ${BLKSIZE}))
  139. ROOTUSED=$(((${DFKSIZE} - ${DFKFREE}) * 1024))
  140. IRFSMIN=$(((${ROOTUSED} + (${ADDBLK} * ${BLKSIZE}) + (${ONEMB} - 1)) / ${ONEMB}))
  141. IRFSMAX=$(((${ROOTSIZE} + (${ONEMB} - 1)) / ${ONEMB}))
  142. IMGFILE="$1"
  143. HOST="$(hostname)"
  144. MON="$(date +%B)"
  145. YR="$(date +%Y)"
  146. if [ "${IMGFILE}" = "" ]; then
  147. while :
  148. do
  149. echo ""
  150. read -r -e -i "${IMGFILE}" -p "Image file to create? ( $NSHARE/BackupImage-$HOST$MON$YR )" IMGFILE
  151. if [ "${IMGFILE}" = "" ]; then
  152. continue
  153. elif [ -d "${IMGFILE}" ]; then
  154. echo ""
  155. echo "${IMGFILE} is a directory"
  156. elif [ -f "${IMGFILE}" ]; then
  157. echo ""
  158. echo -n "${IMGFILE} already exists, Ok to delete (y/n)? "
  159. while read -r -n 1 -s answer; do
  160. if [[ "${answer}" = [yYnN] ]]; then
  161. echo "${answer}"
  162. if [[ "${answer}" = [yY] ]]; then
  163. break 2
  164. else
  165. break 1
  166. fi
  167. fi
  168. done
  169. else
  170. break
  171. fi
  172. done
  173. IRFSSIZE=""
  174. while :
  175. do
  176. echo ""
  177. read -r -e -i "${IRFSSIZE}" -p "Image ROOT filesystem size (MB) [${IRFSMAX}]? " IRFSSIZE
  178. if [ "${IRFSSIZE}" = "" ]; then
  179. IRFSSIZE=${IRFSMAX}
  180. break
  181. elif [ ${IRFSSIZE} -ge ${IRFSMIN} ]; then
  182. break
  183. else
  184. echo ""
  185. echo "Requested image ROOT filesystem size (${IRFSSIZE}) is too small (Minimum = ${IRFSMIN})"
  186. IRFSSIZE=${IRFSMIN}
  187. fi
  188. done
  189. echo ""
  190. echo -n "Create ${IMGFILE} [${IRFSSIZE} MB] (y/n)? "
  191. while read -r -n 1 -s answer; do
  192. if [[ "${answer}" = [yYnN] ]]; then
  193. echo "${answer}"
  194. if [[ "${answer}" = [yY] ]]; then
  195. break
  196. else
  197. errexit "$HOST - Aborted"
  198. fi
  199. fi
  200. done
  201. if [ -f "${IMGFILE}" ]; then
  202. rm "${IMGFILE}"
  203. if [ $? -ne 0 ]; then
  204. errexit "$HOST - Unable to delete existing image file"
  205. fi
  206. fi
  207. ROOTEND=$((${ROOTBEG} + ((${IRFSSIZE} * ${ONEMB}) / 512) - 1))
  208. truncate -s $(((${ROOTEND} + 1) * 512)) "${IMGFILE}"
  209. if [ $? -ne 0 ]; then
  210. errexit "$HOST - Unable to create image file"
  211. fi
  212. fdisk "${IMGFILE}" <<EOF > /dev/null
  213. p
  214. n
  215. p
  216. 1
  217. ${BOOTBEG}
  218. ${BOOTEND}
  219. t
  220. c
  221. p
  222. n
  223. p
  224. 2
  225. ${ROOTBEG}
  226. ${ROOTEND}
  227. p
  228. w
  229. EOF
  230. mkloop1
  231. mkfs.vfat "${LOOP1}" > /dev/null
  232. if [ $? -ne 0 ]; then
  233. errexit "$HOST - Unable to create image BOOT filesystem (vfat)"
  234. fi
  235. dosfsck "${LOOP1}" > /dev/null
  236. if [ $? -ne 0 ]; then
  237. errexit "$HOST - Image BOOT filesystem appears corrupted"
  238. fi
  239. rmloop1
  240. mkloop2
  241. mkfs.ext4 -q -b ${BLKSIZE} "${LOOP2}" > /dev/null
  242. if [ $? -ne 0 ]; then
  243. errexit "$HOST - Unable to create image ROOT filesystem (ext4)"
  244. fi
  245. rmloop2
  246. fdisk "${IMGFILE}" <<EOF > /dev/null
  247. p
  248. x
  249. i
  250. 0x${PTUUID}
  251. r
  252. p
  253. w
  254. EOF
  255. mntimg
  256. mkdir "${MNTPATH}"/dev/ "${MNTPATH}"/media/ "${MNTPATH}"/mnt/ "${MNTPATH}"/proc/ "${MNTPATH}"/run/ "${MNTPATH}"/sys/ "${MNTPATH}"/tmp/
  257. if [ $? -ne 0 ]; then
  258. errexit "$HOST - Unable to create image directories"
  259. fi
  260. chmod a+rwxt "${MNTPATH}"/tmp/
  261. umntimg
  262. echo ""
  263. echo "Starting $HOST full backup. For incremental backups, run: $0 ${IMGFILE}"
  264. elif [ -d "${IMGFILE}" ]; then
  265. errexit "$HOST - ${IMGFILE} is a directory"
  266. elif [ ! -f "${IMGFILE}" ]; then
  267. errexit "$HOST - ${IMGFILE} not found"
  268. fi
  269. mntimg
  270. sync
  271. 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' \
  272. --exclude '/tmp' --exclude 'lost\+found' --exclude '/etc/udev/rules.d/70-persistent-net.rules' --exclude '/var/lib/asterisk/astdb.sqlite3-journal' / "${MNTPATH}"/
  273. if [ $? -ne 0 ]; then
  274. errexit "$HOST - Unable to create backup"
  275. fi
  276. sync
  277. umntimg
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement