diskimg 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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:DhjJPr: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. while true ; do
  81. case "$1" in
  82. -d) DISK_DEVICE="$2" ; shift ; shift
  83. ;;
  84. -c) TARGET_DIR="$2" ; shift ; shift
  85. ;;
  86. -e) EXCLUDE_PATTERNS="$EXCLUDE_PATTERNS $2" ; shift ; shift
  87. ;;
  88. -r) SOURCE_DIR="$2" ; shift ; shift
  89. ;;
  90. -u) COMPRESSION_ALGORITHM="" ; COMPRESSION_SUFFIX="" ; shift
  91. ;;
  92. -z) COMPRESSION_ALGORITHM="-z" ; COMPRESSION_SUFFIX=".gz" ; shift
  93. ;;
  94. -j) COMPRESSION_ALGORITHM="-j"; COMPRESSION_SUFFIX=".bzip2" ; shift
  95. ;;
  96. -v) VERBOSE="1"; VERBOSE_OPTION="-v" ; shift
  97. ;;
  98. -V) VERBOSE="1"
  99. VERBOSE_OPTION="-v"
  100. set -x
  101. shift
  102. ;;
  103. -D) DEBUG="1"; shift
  104. ;;
  105. -P) KEEP_PARTITIONS="1"; shift
  106. ;;
  107. -T) SKIP_TAR="1"; shift
  108. ;;
  109. -h|--help)
  110. printHelp
  111. exit 0
  112. ;;
  113. --) shift; break
  114. ;;
  115. esac
  116. done
  117. ESCAPED_COMPRESSION_SUFFIX="\$COMPRESSION_SUFFIX"
  118. create_exclude_string(){
  119. EXCLUDE_OPTION="--exclude=."
  120. EXCLUDE_STRING=""
  121. for pattern in $(echo $EXCLUDE_PATTERNS) ; do
  122. EXCLUDE_STRING="$EXCLUDE_STRING $EXCLUDE_OPTION$pattern"
  123. done
  124. }
  125. do_restore_disk(){
  126. echo "Restoring disk image from $SOURCE_DIR to $DISK_DEVICE"
  127. if [[ "$KEEP_PARTITIONS" != "1" ]] ; then
  128. echo "Restoring boot sector backup"
  129. test "1" != "$DEBUG" && dcfldd if=$SOURCE_DIR/boot-sector.img of=$DISK_DEVICE || exit 44
  130. echo "Restoring partition table backup"
  131. test "1" != "$DEBUG" && sfdisk $DISK_DEVICE < $SOURCE_DIR/partition-table.sfdisk || exit 45
  132. echo "Updating from partition information"
  133. test "1" != "$DEBUG" && hdparm -z $DISK_DEVICE
  134. fi
  135. ls $SOURCE_DIR/partition*.tar$COMPRESSION_SUFFIX > /dev/null 2>&1
  136. if [[ "$?" != "0" ]] ; then
  137. echo "There are no files with the suffix .tar$COMPRESSION_SUFFIX in directory $SOURCE_DIR"
  138. echo "Please check you are using the right compression algorithm (options could be -j -J or -z)"
  139. exit 6
  140. fi
  141. #For each partition, we restore the backup
  142. for partition_file in $(ls $SOURCE_DIR/partition*.tar$COMPRESSION_SUFFIX) ; do
  143. partition_dev=$(basename $partition_file | sed "s+partition-+$DISK_DEVICE+" |sed "s/\.tar$ESCAPED_COMPRESSION_SUFFIX//")
  144. backup_basename=$(basename $partition_file)
  145. backup_filename="$backup_basename.tar$COMPRESSION_SUFFIX "
  146. partition_type_file=$SOURCE_DIR/$(echo $backup_basename | sed "s+\.tar$ESCAPED_COMPRESSION_SUFFIX+.type+")
  147. echo ""
  148. echo "--> Restoring backup $partition_file into $partition_dev"
  149. if [[ "$KEEP_PARTITIONS" != "1" ]] ; then
  150. #First get the partition type
  151. partition_type=$(cat $partition_type_file)
  152. echo "Formating $partition_dev with type $partition_type"
  153. test "1" != "$DEBUG" && mkfs -t $partition_type $partition_dev
  154. test "$?" == 0 || exit $?
  155. fi
  156. mount_dir_name="diskimg-$backup_basename"
  157. mountpoint_dir="/mnt/$mount_dir_name"
  158. test "1" = "$VERBOSE" && echo "Mounting $partition_dev to $mountpoint_dir"
  159. mkdir -p $mountpoint_dir
  160. test "$?" == 0 || exit $?
  161. mount $partition_dev $mountpoint_dir
  162. test "$?" == 0 || exit $?
  163. test "1" = "$VERBOSE" && echo "Restoring from $partition_file to $mountpoint_dir"
  164. if [[ "$SKIP_TAR" != 1 ]] ; then
  165. test "1" = "$DEBUG" && echo tar --numeric-owner $COMPRESSION_ALGORITHM $VERBOSE_OPTION -pxf $partition_file -C $mountpoint_dir
  166. test "1" != "$DEBUG" && tar --numeric-owner $COMPRESSION_ALGORITHM $VERBOSE_OPTION -pxf $partition_file -C $mountpoint_dir
  167. fi
  168. test "$?" == 0 || exit $?
  169. test "1" = "$VERBOSE" && echo "Un-mounting $partition_dev"
  170. umount $partition_dev
  171. test "$?" == 0 || exit $?
  172. rmdir $mountpoint_dir
  173. test "$?" == 0 || exit $?
  174. done
  175. }
  176. restore_disk(){
  177. if [[ -d "$SOURCE_DIR" ]] ; then
  178. echo "Restoring disk image from $SOURCE_DIR to $DISK_DEVICE"
  179. echo ""
  180. echo "You will lose any data stored on $DISK_DEVICE"
  181. echo "Please make sure you have a backup"
  182. echo ""
  183. echo "ARE YOU SURE you want to erase any data on $DISK_DEVICE ? (yes/NO)"
  184. read answer
  185. while [[ "$answer" =~ [yY]$ ]] ; do
  186. echo "Please type yes or YES"
  187. read answer
  188. done
  189. if [[ "$answer" =~ yes ]] ; then
  190. do_restore_disk
  191. fi
  192. else
  193. echo "ERROR: $SOURCE_DIR does not exist"
  194. exit 4
  195. fi
  196. }
  197. backup_mounted_volume(){
  198. backup_basename=$1
  199. partition_dev=$2
  200. part_type=$3
  201. part_type_filename="$backup_basename.type"
  202. echo $part_type > $TARGET_DIR/$part_type_filename
  203. backup_filename="$backup_basename.tar$COMPRESSION_SUFFIX "
  204. echo "Creating backup for $partition_dev into $backup_filename"
  205. mount_dir_name="diskimg-$backup_basename"
  206. mountpoint_dir="/mnt/$mount_dir_name"
  207. test "1" = "$VERBOSE" && echo "Mounting $partition_dev to $mountpoint_dir"
  208. mkdir -p $mountpoint_dir
  209. mount $partition_dev $mountpoint_dir
  210. test "1" = "$VERBOSE" && echo "Backing up from $mountpoint_dir to $TARGET_DIR/$backup_filename"
  211. if [[ "$SKIP_TAR" != 1 ]] ; then
  212. test "1" != "$DEBUG" && echo "tar --numeric-owner $COMPRESSION_ALGORITHM $VERBOSE_OPTION $EXCLUDE_STRING -pcf $TARGET_DIR/$backup_filename -C $mountpoint_dir ."
  213. test "1" != "$DEBUG" && tar --numeric-owner $COMPRESSION_ALGORITHM $VERBOSE_OPTION $EXCLUDE_STRING -pcf $TARGET_DIR/$backup_filename -C $mountpoint_dir .
  214. fi
  215. umount $partition_dev
  216. rmdir $mountpoint_dir
  217. }
  218. ensure_crypt_part_is_opened(){
  219. partition_dev=$1
  220. backup_basename=$2
  221. #Are we already opened ? in this case,
  222. #we should see "crypt" string in the output of lsblk -l -n $partition_dev
  223. lsblk -l -n $partition_dev | grep crypt > /dev/null 2>&1
  224. rv=$?
  225. if [ "$rv" != "0" ] ; then
  226. echo '### Please open the crypted volume by providing a passphrase to unlock it ###'
  227. cryptsetup luksOpen $partition_dev $backup_basename
  228. fi
  229. #Now we should get the uncrypted volume name (which is not necessarily the
  230. #same as $backup_basename)
  231. current_vol_name=$(lsblk -l -n $partition_dev |grep crypt | awk '{print $1}')
  232. #Now get the partition type and returns it
  233. part_type=`lsblk -l -n -o NAME,FSTYPE $partition_dev | grep $backup_basename | awk '{print $2}'`
  234. _UNCRYPTED_TYPE=${part_type}
  235. _UNCRYPTED_VOL=/dev/mapper/$current_vol_name
  236. }
  237. backup_disk(){
  238. if [[ ! -d "$TARGET_DIR" ]] ; then
  239. echo "$TARGET_DIR does not exist"
  240. echo "Do you want to create it ? (y/N)"
  241. read answer
  242. if [[ "$answer" =~ [yY] ]] ; then
  243. mkdir -p $TARGET_DIR
  244. else
  245. echo "Aborting because target dir does not exist"
  246. exit 126
  247. fi
  248. fi
  249. if [[ -d "$TARGET_DIR" ]] ; then
  250. echo "Creating disk image from $DISK_DEVICE to $TARGET_DIR..."
  251. echo "Creating boot sector backup"
  252. test "1" != "$DEBUG" && echo dcfldd if=$DISK_DEVICE bs=1M count=1 of=$TARGET_DIR/boot-sector.img
  253. test "1" != "$DEBUG" && dcfldd if=$DISK_DEVICE bs=1M count=1 of=$TARGET_DIR/boot-sector.img
  254. echo "Creating partition table backup"
  255. test "1" != "$DEBUG" && echo sfdisk -d $DISK_DEVICE > $TARGET_DIR/partition-table.sfdisk
  256. test "1" != "$DEBUG" && sfdisk -d $DISK_DEVICE > $TARGET_DIR/partition-table.sfdisk
  257. create_exclude_string
  258. #For each partition, we make a backup
  259. for partition_dev in $(sfdisk -d $DISK_DEVICE | grep -F 'start=' | awk '{print $1}') ; do
  260. backup_basename=$(echo $partition_dev | sed "s+$DISK_DEVICE+partition-+")
  261. #Device to mount will be empty unless necessary (ie: if a crypt volume is to be backup-ed)
  262. device_to_mount=""
  263. #Partition type
  264. echo "Saving partition type"
  265. part_type=`lsblk -lno NAME,FSTYPE $partition_dev | grep $(basename $partition_dev) | awk '{print $2}'`
  266. #part_type=`lsblk -lno NAME,FSTYPE $partition_dev | grep $DISK_DEVICE | print '{print $2}'`
  267. case $part_type in
  268. crypto_LUKS)
  269. #We extra steps here:
  270. #First decipher the luks partition:
  271. echo '###'$part_type'###'
  272. ensure_crypt_part_is_opened $partition_dev $backup_basename
  273. part_type=${_UNCRYPTED_TYPE}
  274. echo '***'$part_type'***'
  275. device_to_mount=${_UNCRYPTED_VOL}
  276. backup_mounted_volume $backup_basename $device_to_mount $part_type
  277. ;;
  278. *)
  279. backup_mounted_volume $backup_basename $partition_dev $part_type
  280. ;;
  281. esac
  282. done
  283. fi
  284. }
  285. if [ -z "$TARGET_DIR" ] && [ -z "$SOURCE_DIR" ]
  286. then
  287. echo "You must indicate an operation with -c or -r"
  288. exit 2
  289. fi
  290. if [ -z "$DISK_DEVICE" ]
  291. then
  292. echo "You must indicate a disk device"
  293. exit 3
  294. fi
  295. if [[ $EUID -ne 0 ]]; then
  296. echo "This script cannot work if not super-user. Please run as root" 1>&2
  297. exit 1
  298. fi
  299. if [[ "$TARGET_DIR" != "" ]] ; then
  300. backup_disk
  301. exit
  302. fi
  303. if [[ "$SOURCE_DIR" != "" ]] ; then
  304. restore_disk
  305. exit
  306. fi