[HowTo] Linux script to make backing up your SD card easier

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 named OSMCSDBACKUP-(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.


#Adjust this to the location of your SD Card. ie. /dev/sdb (Don't include partition numbers)

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
    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
    if [ -e $devFull ]
            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
	    echo -e "\033[1mDevice "$devFull" does not exist in your system!\033[0m"

Please share any improvements you make! Enjoy.

Update: Borrowed some code from @Toast to make the script more flexible. Thanks Toast!

I am not 100% sure what are you trying to achieve as I don’t know your exact setup. But do I understand it correctly that you want to make a image of your SD Card while being logged in OSMC via SSH?

No. This script has to be run from a separate computer with your OSMC SD card in a card reader. I was just about to make note of that in my post.

Ok, than it makes more sense.
Are you sure that the wildcard in the unmount command works? I have my doubts about that.

Well I’ve tested it and it works, so it has that going for it. If you can suggest a more elegant way, I’ll happily switch.

Well it says

Unmounting /dev/sdb
umount: /dev/sdb: not mounted

So either your partition (sdb1) was not mounted anyhow or the script success without unmounting.

I think it’s just throwing an error because /dev/sdb wasn’t mounted, but the * at the end went on to umount /dev/sdb1. If there was a way to umount /dev/sdb1-9 without trying to also umount /dev/sdb that would be ideal but I don’t know how.

umount /dev/dvb{1,2,3,4,5,6,7,8,9} possibly

I just did some testing and it turns out ? works without throwing the error. Updating script.

Or as you have a script anyhow for n in /dev/sdb* ; do umount $n ; done

I’m still at the toddler stage of scripting. Thanks for the help, guys.

@Katze check out one of my older scripts for installing arch linux might be some gems there in on how to define what drive etc to use

Thanks @Toast. I’ll have some fun playing with that. :sunglasses:

Looks nice :smile: glad to see my older work come to use :smile: