diskimg 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. #!/bin/bash
  2. # This script allows backuping a complete SD card, NAND or eMMC card using tar utility
  3. #
  4. _resolve_file_location(){
  5. SOURCE="$1"
  6. while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
  7. THE_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
  8. SOURCE="$(readlink "$SOURCE")"
  9. [[ "$SOURCE" != "/*" ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
  10. done
  11. THE_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
  12. echo $THE_DIR
  13. }
  14. _get_relative_path_to(){
  15. source=$1
  16. target=$2
  17. common_part=$source
  18. back=
  19. while [ "${target#$common_part}" = "${target}" ]; do
  20. common_part=$(dirname $common_part)
  21. back="../${back}"
  22. done
  23. echo ${back}${target#$common_part/}
  24. }
  25. printHelp(){
  26. cat << EOF
  27. NAME:
  28. diskimg -- Creates or restores a partition
  29. SYNOPSIS:
  30. $0 -c TARGET_DIR -d DISK_DEVICE [-v]
  31. $0 -s SOURCE_DIR -d DISK_DEVICE [-v]
  32. DESCRIPTION:
  33. Creates a backup of the given device using the `-c` option. All partition
  34. table, boot sector, partition contents are stored into the given target
  35. directory in different files.
  36. Restores the backup using the `-r` option to the given device. All partition
  37. table, boot sector, partition contents are restored from the given target
  38. directory
  39. OPTIONS:
  40. -c creates the backup using the given TARGET_DIR value
  41. -d the DISK_DEVICE from/to process
  42. -e exclude file patterns from tar (creation). Re-use for several values (e.g -e /foo/bar -e /my/path)
  43. -u uncompressed image (disables compression)
  44. -z uses gzip compression
  45. -j uses bzip2 compression
  46. -P keep existing partitions
  47. -r restores from the data stored into the SOURCE_DIR value
  48. -J uses XZ compression (DEFAULT)
  49. -T skip tar archive creation (useful if backup only partition layout)
  50. -v Verbose
  51. -v Very verbose (debug mode)
  52. -D dry run
  53. EXAMPLES:
  54. One can easily recreate the partition scheme using using the following command
  55. (which will not write the data from the tar files) :
  56. $0 -r /my/path/to/backup -d /dev/sdb -T
  57. Then it is possible to change the layout (resize filesystem to partition size e.g.)
  58. And then to restore the data (option -P will preserve existing partitions):
  59. $0 -r /my/path/to/backup -d /dev/sdb -P
  60. EOF
  61. exit 0
  62. }
  63. OPTS=$(getopt c:d:e:FDhjJPr:uTvVz $*)
  64. #Test les paramètres
  65. if [ $? != 0 ]
  66. then
  67. echo "Error while retrieving paramger data from getopt"
  68. printHelp
  69. exit 1
  70. fi
  71. eval set -- "$OPTS"
  72. DISK_DEVICE=""
  73. TARGET_DIR=""
  74. SOURCE_DIR=""
  75. COMPRESSION_ALGORITHM="-J"
  76. COMPRESSION_SUFFIX=".xz"
  77. VERBOSE_OPTION=""
  78. EXCLUDE_PATTERNS=""
  79. KEEP_PARTITIONS=""
  80. FAKE_DEVICE=""
  81. while true ; do
  82. case "$1" in
  83. -d) DISK_DEVICE="$2" ; shift ; shift
  84. ;;
  85. -c) TARGET_DIR="$2" ; shift ; shift
  86. ;;
  87. -e) EXCLUDE_PATTERNS="$EXCLUDE_PATTERNS $2" ; shift ; shift
  88. ;;
  89. -F) FAKE_DEVICE="1"; shift
  90. ;;
  91. -r) SOURCE_DIR="$2" ; shift ; shift
  92. ;;
  93. -u) COMPRESSION_ALGORITHM="" ; COMPRESSION_SUFFIX="" ; shift
  94. ;;
  95. -z) COMPRESSION_ALGORITHM="-z" ; COMPRESSION_SUFFIX=".gz" ; shift
  96. ;;
  97. -j) COMPRESSION_ALGORITHM="-j"; COMPRESSION_SUFFIX=".bzip2" ; shift
  98. ;;
  99. -v) VERBOSE="1"; VERBOSE_OPTION="-v" ; shift
  100. ;;
  101. -V) VERBOSE="1"
  102. VERBOSE_OPTION="-v"
  103. set -x
  104. shift
  105. ;;
  106. -D) DEBUG="1"; shift
  107. ;;
  108. -P) KEEP_PARTITIONS="1"; shift
  109. ;;
  110. -T) SKIP_TAR="1"; shift
  111. ;;
  112. -h|--help)
  113. printHelp
  114. exit 0
  115. ;;
  116. --) shift; break
  117. ;;
  118. esac
  119. done
  120. ESCAPED_COMPRESSION_SUFFIX="\$COMPRESSION_SUFFIX"
  121. create_exclude_string(){
  122. EXCLUDE_OPTION="--exclude=."
  123. EXCLUDE_STRING=""
  124. for pattern in $(echo $EXCLUDE_PATTERNS) ; do
  125. EXCLUDE_STRING="$EXCLUDE_STRING $EXCLUDE_OPTION$pattern"
  126. done
  127. }
  128. do_restore_disk(){
  129. echo "Restoring disk image from $SOURCE_DIR to $DISK_DEVICE"
  130. if [[ "$KEEP_PARTITIONS" != "1" ]] ; then
  131. echo "Restoring boot sector backup"
  132. if [[ "1" != "$DEBUG" ]] ; then
  133. dcfldd if=$SOURCE_DIR/boot-sector.img of=$DISK_DEVICE || exit 44
  134. fi
  135. echo "Restoring partition table backup"
  136. if [[ "1" != "$DEBUG" ]] ; then
  137. sfdisk $DISK_DEVICE < $SOURCE_DIR/partition-table.sfdisk || exit 45
  138. fi
  139. echo "Updating from partition information"
  140. if [[ "1" != "$DEBUG" ]] ; then
  141. hdparm -z $DISK_DEVICE
  142. fi
  143. fi
  144. ls $SOURCE_DIR/partition*.tar$COMPRESSION_SUFFIX > /dev/null 2>&1
  145. if [[ "$?" != "0" ]] ; then
  146. echo "There are no files with the suffix .tar$COMPRESSION_SUFFIX in directory $SOURCE_DIR"
  147. echo "Please check you are using the right compression algorithm (options could be -j -J or -z)"
  148. exit 6
  149. fi
  150. #For each partition, we restore the backup
  151. for partition_file in $(ls $SOURCE_DIR/partition*.tar$COMPRESSION_SUFFIX) ; do
  152. partition_dev=$(basename $partition_file | sed "s+partition-+$DISK_DEVICE+" |sed "s/\.tar$ESCAPED_COMPRESSION_SUFFIX//")
  153. backup_basename=$(basename $partition_file)
  154. backup_filename="$backup_basename.tar$COMPRESSION_SUFFIX "
  155. partition_type_file=$SOURCE_DIR/$(echo $backup_basename | sed "s+\.tar$ESCAPED_COMPRESSION_SUFFIX+.type+")
  156. echo ""
  157. echo "--> Restoring backup $partition_file into $partition_dev"
  158. if [[ "$KEEP_PARTITIONS" != "1" ]] ; then
  159. #First get the partition type
  160. partition_type=$(cat $partition_type_file)
  161. echo "Formating $partition_dev with type $partition_type"
  162. if [[ "1" != "$DEBUG" ]] ; then
  163. mkfs -t $partition_type $partition_dev
  164. test "$?" == 0 || exit $?
  165. fi
  166. fi
  167. mount_dir_name="diskimg-$backup_basename"
  168. mountpoint_dir="/mnt/$mount_dir_name"
  169. test "1" = "$VERBOSE" && echo "Mounting $partition_dev to $mountpoint_dir"
  170. [[ "$FAKE_DEVICE" != "1" ]] && mkdir -p $mountpoint_dir
  171. test "$?" == 0 || exit $?
  172. mount $partition_dev $mountpoint_dir
  173. test "$?" == 0 || exit $?
  174. test "1" = "$VERBOSE" && echo "Restoring from $partition_file to $mountpoint_dir"
  175. if [[ "$SKIP_TAR" != 1 ]] ; then
  176. test "1" = "$DEBUG" && echo tar --numeric-owner $COMPRESSION_ALGORITHM $VERBOSE_OPTION -pxf $partition_file -C $mountpoint_dir
  177. test "1" != "$DEBUG" && tar --numeric-owner $COMPRESSION_ALGORITHM $VERBOSE_OPTION -pxf $partition_file -C $mountpoint_dir
  178. fi
  179. test "$?" == 0 || exit $?
  180. test "1" = "$VERBOSE" && echo "Un-mounting $partition_dev"
  181. if [[ "$FAKE_DEVICE" != "1" ]] ; then
  182. umount $partition_dev
  183. test "$?" == 0 || exit $?
  184. rmdir $mountpoint_dir
  185. test "$?" == 0 || exit $?
  186. fi
  187. done
  188. }
  189. restore_disk(){
  190. if [[ -d "$SOURCE_DIR" ]] ; then
  191. echo "Restoring disk image from $SOURCE_DIR to $DISK_DEVICE"
  192. echo ""
  193. echo "You will lose any data stored on $DISK_DEVICE"
  194. echo "Please make sure you have a backup"
  195. echo ""
  196. echo "ARE YOU SURE you want to erase any data on $DISK_DEVICE ? (yes/NO)"
  197. read answer
  198. while [[ "$answer" =~ [yY]$ ]] ; do
  199. echo "Please type yes or YES"
  200. read answer
  201. done
  202. if [[ "$answer" =~ yes ]] ; then
  203. do_restore_disk
  204. fi
  205. else
  206. echo "ERROR: $SOURCE_DIR does not exist"
  207. exit 4
  208. fi
  209. }
  210. backup_mounted_volume(){
  211. backup_basename=$1
  212. partition_dev=$2
  213. part_type=$3
  214. part_type_filename="$backup_basename.type"
  215. echo $part_type > $TARGET_DIR/$part_type_filename
  216. backup_filename="$backup_basename.tar$COMPRESSION_SUFFIX "
  217. echo "--- Creating backup for $partition_dev into $backup_filename ---"
  218. mount_dir_name="diskimg-$backup_basename"
  219. mountpoint_dir="/mnt/$mount_dir_name"
  220. test "1" = "$VERBOSE" && echo "Mounting $partition_dev to $mountpoint_dir"
  221. mkdir -p $mountpoint_dir
  222. mount $partition_dev $mountpoint_dir
  223. test "1" = "$VERBOSE" && echo "Backing up from $mountpoint_dir to $TARGET_DIR/$backup_filename"
  224. if [[ "$SKIP_TAR" != 1 ]] ; then
  225. test "1" != "$DEBUG" && echo "tar --numeric-owner $COMPRESSION_ALGORITHM $VERBOSE_OPTION $EXCLUDE_STRING -pcf $TARGET_DIR/$backup_filename -C $mountpoint_dir ."
  226. test "1" != "$DEBUG" && tar --numeric-owner $COMPRESSION_ALGORITHM $VERBOSE_OPTION $EXCLUDE_STRING -pcf $TARGET_DIR/$backup_filename -C $mountpoint_dir .
  227. fi
  228. umount $partition_dev
  229. rmdir $mountpoint_dir
  230. }
  231. ensure_crypt_part_is_opened(){
  232. partition_dev=$1
  233. backup_basename=$2
  234. #Are we already opened ? in this case,
  235. #we should see "crypt" string in the output of lsblk -l -n $partition_dev
  236. lsblk -l -n $partition_dev | grep crypt > /dev/null 2>&1
  237. rv=$?
  238. if [ "$rv" != "0" ] ; then
  239. echo '### Please open the crypted volume by providing a passphrase to unlock it ###'
  240. cryptsetup luksOpen $partition_dev $backup_basename
  241. fi
  242. #Now we should get the uncrypted volume name (which is not necessarily the
  243. #same as $backup_basename)
  244. current_vol_name=$(lsblk -l -n $partition_dev |grep crypt | awk '{print $1}')
  245. #Now get the partition type and returns it
  246. part_type=`lsblk -l -n -o NAME,FSTYPE $partition_dev | grep $backup_basename | awk '{print $2}'`
  247. _UNCRYPTED_TYPE=${part_type}
  248. _UNCRYPTED_VOL=/dev/mapper/$current_vol_name
  249. }
  250. backup_disk(){
  251. if [[ ! -d "$TARGET_DIR" ]] ; then
  252. echo "$TARGET_DIR does not exist"
  253. echo "Do you want to create it ? (y/N)"
  254. read answer
  255. if [[ "$answer" =~ [yY] ]] ; then
  256. mkdir -p $TARGET_DIR
  257. else
  258. echo "Aborting because target dir does not exist"
  259. exit 126
  260. fi
  261. fi
  262. if [[ -d "$TARGET_DIR" ]] ; then
  263. echo "Creating disk image from $DISK_DEVICE to $TARGET_DIR..."
  264. echo "Creating boot sector backup"
  265. test "1" != "$DEBUG" && echo dcfldd if=$DISK_DEVICE bs=1M count=1 of=$TARGET_DIR/boot-sector.img
  266. test "1" != "$DEBUG" && dcfldd if=$DISK_DEVICE bs=1M count=1 of=$TARGET_DIR/boot-sector.img
  267. echo "Creating partition table backup"
  268. test "1" != "$DEBUG" && echo sfdisk -d $DISK_DEVICE > $TARGET_DIR/partition-table.sfdisk
  269. test "1" != "$DEBUG" && sfdisk -d $DISK_DEVICE > $TARGET_DIR/partition-table.sfdisk
  270. create_exclude_string
  271. #For each partition, we make a backup
  272. for partition_dev in $(sfdisk -d $DISK_DEVICE | grep -F 'start=' | awk '{print $1}') ; do
  273. backup_basename=$(echo $partition_dev | sed "s+$DISK_DEVICE+partition-+")
  274. #Device to mount will be empty unless necessary (ie: if a crypt volume is to be backup-ed)
  275. device_to_mount=""
  276. #Partition type
  277. echo "Saving partition type"
  278. part_type=`lsblk -lno NAME,FSTYPE $partition_dev | grep $(basename $partition_dev) | awk '{print $2}'`
  279. case $part_type in
  280. crypto_LUKS)
  281. #We extra steps here:
  282. #First decipher the luks partition:
  283. ensure_crypt_part_is_opened $partition_dev $backup_basename
  284. #The inner partition and device may have changed
  285. part_type=${_UNCRYPTED_TYPE}
  286. device_to_mount=${_UNCRYPTED_VOL}
  287. backup_mounted_volume $backup_basename $device_to_mount $part_type
  288. ;;
  289. *)
  290. backup_mounted_volume $backup_basename $partition_dev $part_type
  291. ;;
  292. esac
  293. done
  294. fi
  295. }
  296. if [ -z "$TARGET_DIR" ] && [ -z "$SOURCE_DIR" ]
  297. then
  298. echo "You must indicate an operation with -c or -r"
  299. exit 2
  300. fi
  301. if [ -z "$DISK_DEVICE" ]
  302. then
  303. echo "You must indicate a disk device"
  304. exit 3
  305. fi
  306. if [[ $EUID -ne 0 ]]; then
  307. echo "This script cannot work if not super-user. Please run as root" 1>&2
  308. exit 1
  309. fi
  310. if [[ "$TARGET_DIR" != "" ]] ; then
  311. backup_disk
  312. exit
  313. fi
  314. if [[ "$SOURCE_DIR" != "" ]] ; then
  315. restore_disk
  316. exit
  317. fi