Quick and Easy Rasp Pi 2 Backup and Restore Script

A few of you have asked me what I use to backup my microSD card so here it is
sudo cat /dev/sdX | pv | gzip -c > $( date ‘+%m-%d-%Y’ ).pi2backup.img.gz

All I have to do is shutdown my Pi2, unplug it, and pull the microSD card and place it into my Ubuntu MATE desktop

If I remember I had to install pv. Not default on Ubuntu so not default on Ubuntu MATE, other than that, let me explain the command…

to install pv, simply run “sudo apt-get install pv”

sudo cat /dev/sdX
the X will change depending on your device, I use gparted to discover what my microSD card is. On my machine, it is always /dev/sdd

| pv |
This little part is AWESOME!!! This allows me to know how much data i’ve read with cat, shows me how fast the data is being read as well as how long its been running

gzip -c > $( date ‘+%m-%d-%Y’ ).pi2backup.img.gz
This part here takes the data being read from cat, passed though pv to so I can see how the transfer is working and compresses it via gzip. It even creates an automatic name for me that makes sense… If I was to run this script on October 18, 2015… I would get a file name called 10-18-2015.pi2backup.img.gz

7.42GB flash drive is backed up and compressed in around 7 minutes and is 1.5GB in size once it is done

If I ever break the OS and need to restore the image, the following command is what I used… Sadly I have to say I’ve had to use this 3 times at the time of writing this post

gunzip -dc ImageNameGoesHere.img.gz | pv | sudo dd of=/dev/sdX

MAKE SURE 100% that the target that dd will write to IS your microSD card… If you mess this up, your gonna mess up something and hate me… MEASURE TWICE and cut once always when using dd to write data. DD has the name of data destroyer to many people that have done this for a long time… You agree Wimpy?

Cool thing about this process and these commands for the image creation and compression all take place at the same time… If you do need your backup restored, there is no need to decompress, wait, and then use dd or ddrescue to write it. You can read the gzip file and write it all at the same time… Makes life easier for me, hope it will help you out too

Questions? Feel free to ask… Comments? Feel free to post…

Monery