Last active
August 16, 2021 11:43
-
-
Save dev01d/2ef087de2134e034e9e9afcf46772923 to your computer and use it in GitHub Desktop.
Raspberry Pi SD card backup
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
#!/usr/bin/env bash | |
#Sudo check | |
if (( $EUID != 0 )); then | |
echo -e "\nPlease run as root or use sudo\n" | |
exit | |
fi | |
# OS checks | |
arch=$(uname -s) | |
case $arch in | |
Darwin*) | |
listDisk='diskutil list' | |
;; | |
Linux*) | |
listDisk='lsblk' | |
;; | |
*) | |
exit | |
;; | |
esac | |
# User instruction | |
eval $listDisk | |
echo -e '\e[1;25;34mExample: disk2 or sdb2\e[0m\n' | |
read -p 'Disk to be copied?: ' diskVar | |
# Main attraction | |
echo -e "\e[1;25;33m" | |
read -p 'Are you sure? [Y/n] ' response | |
case "${response:-y}" in | |
y|Y ) | |
umount /dev/$diskVar* 2>/dev/null | |
echo -e "\e[1;25;32m" | |
# This because rdisk is amazing | |
# Prefer ddrescue if available | |
case $arch in | |
Darwin*) | |
if hash ddrescue 2>/dev/null; then | |
ddrescue /dev/r$diskVar $HOME/Downloads/piBak`date +%m-%d-%y`.img | |
else | |
dd if=/dev/r$diskVar of=$HOME/Downloads/piBak`date +%m-%d-%y`.img bs=100M | |
fi | |
;; | |
Linux*) | |
if hash ddrescue 2>/dev/null; then | |
ddrescue /dev/$diskVar $HOME/Downloads/piBak`date +%m-%d-%y`.img | |
else | |
dd if=/dev/$diskVar of=$HOME/Downloads/piBak`date +%m-%d-%y`.img bs=100M | |
fi | |
;; | |
esac | |
;; | |
n|N ) | |
echo 'Ok, maybe later.' ;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment