Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@drewlustro
Created September 8, 2016 00:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drewlustro/a37351acf8bd96a54f985a0dc1d3e43e to your computer and use it in GitHub Desktop.
Save drewlustro/a37351acf8bd96a54f985a0dc1d3e43e to your computer and use it in GitHub Desktop.
Dump/Restore Raspberry Pi SD card image on OS X terminal
# subset of functions.sh from https://github.com/drewlustro/dotfiles
# put in your .bash_profile or .zshrc
function toolbelt-raspi-image-dump() {
local now=$(date +"%Y-%m-%d__%H-%M-%S");
local host=${2:="raspi"};
local diskNumber=${1:--1}
local bs="1M"
local cmd="sudo dd if=/dev/rdisk$1 bs=$bs | gzip > ~/Desktop/$host-$now.pi.gz"
local usage="Usage: $0 [sdCardDiskN] [hostname='raspi']"
if [ $# -eq 0 ]; then
echo $usage;
return 1;
elif $(diskutil list disk$diskNumber | grep -iq Apple); then
echo "Warning: Apple filesystem detected on disk $1. This is probably your boot disk or external HDD, dummy. Format to FAT32.";
return 1;
fi;
echo "Will run '$cmd'";
echo -n "Are you sure? (y/N): "
local answer;
answer=$(bash -c "read -n 1 c; echo \$c");
if echo "$answer" | grep -iq "^y"; then
echo "\n\nDumping card $diskNumber..."
eval $cmd;
if [[ $? -eq 0 ]]; then
echo "Success.";
return 0;
else
echo "[error] Dump failed."
return 1;
fi
fi
echo "\nAborted.";
return 1;
}
function toolbelt-raspi-image-restore() {
local diskNumber=${1:--1}
local bs="1M"
local cmd="gzip -dc $2 | sudo dd of=/dev/rdisk$1 bs=$bs"
local usage="Usage: $0 [targetDiskN] [compressedImageFile]"
if [[ $# -lt 2 ]]; then
echo $usage;
return 1;
elif $(diskutil list disk$diskNumber | grep -iq Apple); then
echo "Warning: Apple filesystem detected on disk $1. This is probably your boot disk or external HDD, dummy. Format to FAT32.";
return 1;
fi;
echo "Will run '$cmd'";
echo -n "Are you sure? (y/N): "
local answer;
answer=$(bash -c "read -n 1 c; echo \$c");
if echo "$answer" | grep -iq "^y"; then
echo "\n\nRestoring card $(basename $2) to disk $diskNumber..."
sudo diskutil unmountDisk /dev/disk$diskNumber;
eval $cmd;
if [[ $? -eq 0 ]]; then
echo "Success.";
return 0;
else
echo "[error] Restore failed."
return 1;
fi
fi
echo "\nAborted.";
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment