Created
September 8, 2016 00:22
-
-
Save drewlustro/a37351acf8bd96a54f985a0dc1d3e43e to your computer and use it in GitHub Desktop.
Dump/Restore Raspberry Pi SD card image on OS X terminal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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