I haven’t been backing up my SD card nearly as often as is wise so I wanted to spend a little time now to make it easier to do in the future.
I’ve written a simple bash script to automate the process and make it a bit safer.
-
The script will run fdisk -l on the device you have set in the
DEVICE=
section of the script to make sure it’s actually looking at your SD card before actually doing anything. -
If the information is correct and you answer
Y
it will umount the device and begin making an .img file of the SD card namedOSMCSDBACKUP-(date and time).img
in your ~/ home folder. -
If your device isn’t listed and you answer
N
all the devices on your system will be listed and you will be prompted for the correct device location. The script then continues making an image of the device as described above.
This script must be run from another computer with your OSMC SD card plugged in via card reader. You can’t run this from your OSMC installation on the Pi.
#!/bin/bash
#Adjust this to the location of your SD Card. ie. /dev/sdb (Don't include partition numbers)
DEVICE=/dev/sdb
echo -e "\033[1mRunning fdisk -l on $DEVICE. Make sure you're running this script with sudo.\033[0m"
fdisk -l $DEVICE
echo " "
echo -ne "\033[1mAre the SD card partitons you want to backup listed above? (Y/N)\033[0m "
read answer
if echo "$answer" | grep -iq "^Y" ;then
echo Unmounting $DEVICE?
umount $DEVICE?
echo -e "\033[1mSaving image of $DEVICE as ~/OSMCSDBACKUP-`date +%m.%d.%y-%H.%M.%S`.img\033[0m"
dd if=$DEVICE of=~/OSMCSDBACKUP-`date +%m.%d.%y-%H.%M.%S`.img bs=4M
else
echo -e "\033[1mListing all devices and partitions on your system.\033[0m"
fdisk -l
echo -ne "\033[1mPlease enter your SD card's location from the list above. (for /dev/sdb enter just sdb) : \033[0m"
read dev
devFull='/dev/'$dev
if [ -e $devFull ]
then
echo -e "\033[1mUnmouting partitions if they have been mounted.\033[0m"
umount /dev/$dev?
echo "Done."
echo -e "\033[1mSaving image of $devFull as ~/OSMCSDBACKUP-`date +%m.%d.%y-%H.%M.%S`.img\033[0m"
dd if=$devFull of=~/OSMCSDBACKUP-`date +%m.%d.%y-%H.%M.%S`.img bs=4M
else
echo -e "\033[1mDevice "$devFull" does not exist in your system!\033[0m"
fi
fi
Please share any improvements you make! Enjoy.
Update: Borrowed some code from @Toast to make the script more flexible. Thanks Toast!