#!/bin/bash # # Copy a good Raspbian .img or Beaglebone img.xz to an SD writer # Alternatively, back up or copy an image # # Usage: ddimage [-b] [-c] [-i imgname ] [device-tag] mode=copy image="" while getopts bci:n opt do case $opt in b) mode=backup ;; c) mode=copy ;; # default mode i) image=$OPTARG ;; esac done shift $(($OPTIND - 1)) target=/dev/${1-sdd} echo "Checking $target" if ls $target >/dev/null 2>&1 then if [ -b $target ] then echo "$target exists as a storage device." else echo "$target is not a storage device" exit 1 fi else echo "$target does not exist." exit 1 fi if df | grep $target >/dev/null then echo "$target is mounted and cannot be used." exit 1 else echo "$target is not mounted" fi if [ -z "$image" ] then case $mode in copy) set -- *.img if [ $# != 1 ] then echo "Need a unique existing image file, specify with -i." exit 1 else image=`ls *img` fi ;; backup) echo "Specify a unique image file with -i." exit 1 ;; esac fi OPTIONS="bs=4M statusinterval=16" case $mode in copy) if [ -f $image ] then echo "Copying $image..." if [[ $file =~ \.gz$ ]] then # Previously-made backup gunzip $file | dcfldd $OPTIONS of=$target elif [[ $file =~ \.xz$ ]] then # Beaglebone image? xz --decompress --stdout $file | dcfldd $OPTIONS of=$target else # This is the arm for decompressed .img files dcfldd $OPTIONS sizeprobe=if if=$image of=$target fi else echo "No .img file, bailing out!" exit 1 fi ;; backup) if [ -f ${image}.gz ] then echo "Refusing to overwrite ${image}.gz file, bailing out!" exit 1 else echo "Backing up $target to ${image}.gz..." dcfldd statusinterval=16 sizeprobe=if bs=4M if=$target | gzip >${image}.gz fi ;; esac if df | grep $target >/dev/null then echo "Unmounting..." umount fi echo "Done" exit 0