diskimg 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. create_exclude_string(){
  121. EXCLUDE_OPTION="--exclude=."
  122. EXCLUDE_STRING=""
  123. for pattern in $(echo $EXCLUDE_PATTERNS) ; do
  124. EXCLUDE_STRING="$EXCLUDE_STRING $EXCLUDE_OPTION$pattern"
  125. done
  126. }
  127. log_debug(){
  128. if [[ "$DEBUG" = "1" ]] ; then
  129. echo $*
  130. fi
  131. }
  132. format_partition(){
  133. local partition_type="$1"
  134. local target_device="$2"
  135. echo "Formating $target_device with type $partition_type"
  136. if [[ "1" != "$DEBUG" ]] ; then
  137. mkfs -t $partition_type $target_device
  138. test "$?" == 0 || exit $?
  139. fi
  140. _REAL_TARGET_DEVICE=$target_device
  141. }
  142. create_luks_container(){
  143. local target_device="$1"
  144. echo "Creating a new LUKS container"
  145. if [ "$DEBUG" != "1" ] && [ "$FAKE_DEVICE" != "1" ] ; then
  146. cryptsetup luksFormat -c aes-xts-plain64 -s 512 -h sha512 $target_device <<EOF
  147. YES
  148. MOT_DE_PASSE
  149. EOF
  150. fi
  151. }
  152. prepare_partition(){
  153. local partition_type="$1"
  154. local target_device="$2"
  155. local backup_basename=$3
  156. for part_type in $(echo $partition_type) ; do
  157. case $part_type in
  158. crypto_LUKS)
  159. ask_password $target_device
  160. create_luks_container $target_device
  161. open_luks_container $target_device $backup_basename
  162. #Now that the partition is opened, we will format it at next loop:
  163. target_device=$_UNCRYPTED_DEVICE
  164. ;;
  165. *)
  166. format_partition $part_type $target_device
  167. ;;
  168. esac
  169. done
  170. }
  171. restore_partition(){
  172. local partition_file=$1
  173. local partition_dev=$2
  174. local backup_basename=$3
  175. mount_dir_name="diskimg-$backup_basename"
  176. mountpoint_dir="/mnt/$mount_dir_name"
  177. test "1" = "$VERBOSE" && echo "Mounting $partition_dev to $mountpoint_dir"
  178. if [[ "$FAKE_DEVICE" != "1" ]] ; then
  179. mkdir -p $mountpoint_dir
  180. test "$?" == 0 || exit $?
  181. mount $partition_dev $mountpoint_dir
  182. test "$?" == 0 || exit $?
  183. fi
  184. test "1" = "$VERBOSE" && echo "Restoring from $partition_file to $mountpoint_dir"
  185. if [[ "$SKIP_TAR" != 1 ]] ; then
  186. if [[ "1" = "$DEBUG" ]] ; then
  187. echo tar --numeric-owner $COMPRESSION_ALGORITHM $VERBOSE_OPTION -pxf $partition_file -C $mountpoint_dir
  188. if [[ "$FAKE_DEVICE" != "1" ]] ; then
  189. tar --numeric-owner $COMPRESSION_ALGORITHM $VERBOSE_OPTION -pxf $partition_file -C $mountpoint_dir
  190. test "$?" == 0 || exit $?
  191. fi
  192. fi
  193. fi
  194. test "1" = "$VERBOSE" && echo "Un-mounting $partition_dev"
  195. if [[ "$FAKE_DEVICE" != "1" ]] ; then
  196. umount $partition_dev
  197. test "$?" == 0 || exit $?
  198. rmdir $mountpoint_dir
  199. test "$?" == 0 || exit $?
  200. fi
  201. }
  202. do_restore_disk(){
  203. echo "Restoring disk image from $SOURCE_DIR to $DISK_DEVICE"
  204. if [[ "$KEEP_PARTITIONS" != "1" ]] ; then
  205. echo "Restoring boot sector backup"
  206. if [[ "1" != "$DEBUG" ]] ; then
  207. dcfldd if=$SOURCE_DIR/boot-sector.img of=$DISK_DEVICE || exit 44
  208. fi
  209. echo "Restoring partition table backup"
  210. if [[ "1" != "$DEBUG" ]] ; then
  211. sfdisk $DISK_DEVICE < $SOURCE_DIR/partition-table.sfdisk || exit 45
  212. fi
  213. echo "Updating from partition information"
  214. if [[ "1" != "$DEBUG" ]] ; then
  215. hdparm -z $DISK_DEVICE
  216. fi
  217. fi
  218. ls $SOURCE_DIR/partition*.tar$COMPRESSION_SUFFIX > /dev/null 2>&1
  219. if [[ "$?" != "0" ]] ; then
  220. echo "There are no files with the suffix .tar$COMPRESSION_SUFFIX in directory $SOURCE_DIR"
  221. echo "Please check you are using the right compression algorithm (options could be -j -J or -z)"
  222. exit 6
  223. fi
  224. #For each partition, we restore the backup
  225. for partition_file in $(ls $SOURCE_DIR/partition*.tar$COMPRESSION_SUFFIX) ; do
  226. partition_dev=$(basename $partition_file | sed "s+partition-+$DISK_DEVICE+" |sed "s/\.tar$COMPRESSION_SUFFIX//")
  227. backup_basename=$(basename $partition_file | sed "s/\.tar$COMPRESSION_SUFFIX//")
  228. backup_filename="$backup_basename.tar$COMPRESSION_SUFFIX "
  229. partition_type_file=$SOURCE_DIR/$backup_basename.type
  230. log_debug "+++ partition_dev =$partition_dev"
  231. log_debug "+++ backup_basename =$backup_basename"
  232. log_debug "+++ backup_filename =$backup_filename"
  233. log_debug "+++ partition_type_file=$partition_type_file"
  234. echo "--- Preparing backup restoration of $partition_file into $partition_dev ---"
  235. if [[ "$KEEP_PARTITIONS" != "1" ]] ; then
  236. #First get the partition type
  237. partition_type=$(cat $partition_type_file)
  238. prepare_partition "$partition_type" $partition_dev $backup_basename
  239. fi
  240. restore_partition $partition_file $_REAL_TARGET_DEVICE $backup_basename
  241. done
  242. }
  243. restore_disk(){
  244. if [[ -d "$SOURCE_DIR" ]] ; then
  245. echo "Restoring disk image from $SOURCE_DIR to $DISK_DEVICE"
  246. echo ""
  247. echo "You will lose any data stored on $DISK_DEVICE"
  248. echo "Please make sure you have a backup"
  249. echo ""
  250. echo "ARE YOU SURE you want to erase any data on $DISK_DEVICE ? (yes/NO)"
  251. read answer
  252. while [[ "$answer" =~ [yY]$ ]] ; do
  253. echo "Please type yes or YES"
  254. read answer
  255. done
  256. if [[ "$answer" =~ yes ]] ; then
  257. do_restore_disk
  258. fi
  259. else
  260. echo "ERROR: $SOURCE_DIR does not exist"
  261. exit 4
  262. fi
  263. }
  264. backup_mounted_volume(){
  265. backup_basename=$1
  266. partition_dev=$2
  267. my_part_type=$3
  268. backup_filename="$backup_basename.tar$COMPRESSION_SUFFIX "
  269. echo "--- Creating backup for $partition_dev into $backup_filename ---"
  270. mount_dir_name="diskimg-$backup_basename"
  271. mountpoint_dir="/mnt/$mount_dir_name"
  272. test "1" = "$VERBOSE" && echo "Mounting $partition_dev to $mountpoint_dir"
  273. mkdir -p $mountpoint_dir
  274. mount $partition_dev $mountpoint_dir
  275. test "1" = "$VERBOSE" && echo "Backing up from $mountpoint_dir to $TARGET_DIR/$backup_filename"
  276. if [[ "$SKIP_TAR" != 1 ]] ; then
  277. test "1" != "$DEBUG" && echo "tar --numeric-owner $COMPRESSION_ALGORITHM $VERBOSE_OPTION $EXCLUDE_STRING -pcf $TARGET_DIR/$backup_filename -C $mountpoint_dir ."
  278. test "1" != "$DEBUG" && tar --numeric-owner $COMPRESSION_ALGORITHM $VERBOSE_OPTION $EXCLUDE_STRING -pcf $TARGET_DIR/$backup_filename -C $mountpoint_dir .
  279. fi
  280. umount $partition_dev
  281. rmdir $mountpoint_dir
  282. }
  283. ask_password(){
  284. local partition_dev=$1
  285. echo "Enter encrypted partition passphrase for $partition_dev:"
  286. read -s _luks_password
  287. }
  288. open_luks_container(){
  289. local target_device=$1
  290. local backup_basename=$2
  291. if [ "$DEBUG" != "1" ] && [ "$FAKE_DEVICE" != "1" ] ; then
  292. cryptsetup luksOpen $target_device $backup_basename<<EOF
  293. $_luks_password
  294. EOF
  295. fi
  296. _UNCRYPTED_DEVICE=/dev/mapper/$backup_basename
  297. }
  298. ask_and_open_encrypted_part(){
  299. local partition_dev=$1
  300. local backup_basename=$2
  301. #Are we already opened ? in this case,
  302. #we should see "crypt" string in the output of lsblk -l -n $partition_dev
  303. lsblk -l -n $partition_dev | grep crypt > /dev/null 2>&1
  304. rv=$?
  305. if [ "$rv" != "0" ] ; then
  306. ask_password $partition_dev
  307. open_luks_container $partition_dev $backup_basename
  308. fi
  309. }
  310. ensure_crypt_part_is_opened(){
  311. partition_dev=$1
  312. backup_basename=$2
  313. ask_and_open_encrypted_part $partition_dev $backup_basename
  314. #Now we should get the uncrypted volume name (which is not necessarily the
  315. #same as $backup_basename)
  316. current_vol_name=$(lsblk -l -n $partition_dev |grep crypt | awk '{print $1}')
  317. #Now get the partition type and returns it
  318. my_part_type=`lsblk -l -n -o NAME,FSTYPE $partition_dev | grep $backup_basename | awk '{print $2}'`
  319. _UNCRYPTED_TYPE=${my_part_type}
  320. _UNCRYPTED_VOL=/dev/mapper/$current_vol_name
  321. }
  322. backup_disk(){
  323. if [[ ! -d "$TARGET_DIR" ]] ; then
  324. echo "$TARGET_DIR does not exist"
  325. echo "Do you want to create it ? (y/N)"
  326. read answer
  327. if [[ "$answer" =~ [yY] ]] ; then
  328. mkdir -p $TARGET_DIR
  329. else
  330. echo "Aborting because target dir does not exist"
  331. exit 126
  332. fi
  333. fi
  334. if [[ -d "$TARGET_DIR" ]] ; then
  335. echo "Creating disk image from $DISK_DEVICE to $TARGET_DIR..."
  336. echo "Creating boot sector backup"
  337. test "1" != "$DEBUG" && echo dcfldd if=$DISK_DEVICE bs=1M count=1 of=$TARGET_DIR/boot-sector.img
  338. test "1" != "$DEBUG" && dcfldd if=$DISK_DEVICE bs=1M count=1 of=$TARGET_DIR/boot-sector.img
  339. echo "Creating partition table backup"
  340. test "1" != "$DEBUG" && echo sfdisk -d $DISK_DEVICE > $TARGET_DIR/partition-table.sfdisk
  341. test "1" != "$DEBUG" && sfdisk -d $DISK_DEVICE > $TARGET_DIR/partition-table.sfdisk
  342. create_exclude_string
  343. #For each partition, we make a backup
  344. for partition_dev in $(sfdisk -d $DISK_DEVICE | grep -F 'start=' | awk '{print $1}') ; do
  345. backup_basename=$(echo $partition_dev | sed "s+$DISK_DEVICE+partition-+")
  346. #Device to mount will be empty unless necessary (ie: if a crypt volume is to be backup-ed)
  347. device_to_mount=""
  348. #Partition type
  349. echo "Managing partition type"
  350. part_type=`lsblk -lno NAME,FSTYPE $partition_dev | grep $(basename $partition_dev) | awk '{print $2}'`
  351. case $part_type in
  352. crypto_LUKS)
  353. #We extra steps here:
  354. #First decipher the luks partition:
  355. ensure_crypt_part_is_opened $partition_dev $backup_basename
  356. #The inner partition and device may have changed
  357. device_to_mount=${_UNCRYPTED_VOL}
  358. backup_mounted_volume $backup_basename $device_to_mount ${_UNCRYPTED_TYPE}
  359. #Concatenates the part types
  360. part_type="$part_type ${_UNCRYPTED_TYPE}"
  361. ;;
  362. *)
  363. backup_mounted_volume $backup_basename $partition_dev $part_type
  364. ;;
  365. esac
  366. part_type_filename="$backup_basename.type"
  367. echo $part_type > $TARGET_DIR/$part_type_filename
  368. done
  369. fi
  370. }
  371. if [ -z "$TARGET_DIR" ] && [ -z "$SOURCE_DIR" ]
  372. then
  373. echo "You must indicate an operation with -c or -r"
  374. exit 2
  375. fi
  376. if [ -z "$DISK_DEVICE" ]
  377. then
  378. echo "You must indicate a disk device"
  379. exit 3
  380. fi
  381. if [[ $EUID -ne 0 ]]; then
  382. echo "This script cannot work if not super-user. Please run as root" 1>&2
  383. exit 1
  384. fi
  385. if [[ "$TARGET_DIR" != "" ]] ; then
  386. backup_disk
  387. exit
  388. fi
  389. if [[ "$SOURCE_DIR" != "" ]] ; then
  390. restore_disk
  391. exit
  392. fi