Learn how to make a back-up or clone an installation of Raspbian Stretch Lite using RPI-Clone or DD with PiShrink.
If you are running Raspbian Stretch Lite and want to make a backup of your microSD card here are two options for this. First cloning the microSD card onto another microSD card and second, making an .img file that can be used to create a new bootable installation later.
Cloning the microSD Card
If you want to clone your existing installation to another microSD card then RPI-Clone https://github.com/billw2/rpi-clone seems to work the best.
Run the following commands to install rpi-clone:
git clone https://github.com/billw2/rpi-clone.git cd rpi-clone sudo cp rpi-clone rpi-clone-setup /usr/local/sbin
Run a quick test with:
sudo rpi-clone-setup -t testhostname
To clone the current running installation to another microSD card card plugged into an card adapter on the USB port:
You should then be able to power down the Pi and swap the two cards and then power up with the cloned installation. See the Github page above for more example commands.
Making an img File
You can use the Linux dd command to copy the current running installation to an .img file on a removable USB device plugged into the USB port. You can save this somewhere and use it with Etcher to make another bootable microSD card in the future. Take care that you are copying to the correct device. The following commands will create this:
To check the partitions are what you are expecting:
Create the directory for mounting:
sudo mkdir /media/usb
sudo chmod 775 /media/usb
Mount the USB connected device. A single partition Fat32 USB stick on sdb1 in this case:
sudo mount -t vfat /dev/sdb1 /media/usb -o rw,umask=0000
Make an img file from the card inserted in the Pi microSD card slot. Will take some time:
sudo dd if=/dev/mmcblk0 of=/media/usb/imgfile.img bs=1M
This is how it looks on my system:
You can then use another script called PiShrink (https://github.com/Drewsif/PiShrink) to reduce the size of the .img file. PiShrink also has the advantage that it sets up the img so the new installation automatically resizes to fill the microSD card the first time it boots.
To install PiShrink:
wget https://raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.sh chmod +x pishrink.sh sudo mv pishrink.sh /usr/local/bin
sudo pishrink.sh /media/usb/imgfile.img /media/usb/shrunkimgfile.img
When it’s complete you can un-mount the USB device with
sudo umount /dev/sdb1 and remove the USB device.
You can then copy the new shrunk file somewhere for safe keeping. Here’s the two files on my Windows PC
I used Etcher to flash a 16GB card using the shrunkimgfile.img. The original 4GB card image that I copied from has grown to fit the 16GB card when the Pi booted for the first time.
Note: I found that sometimes I would get errors with PiShrink writing to a microSD card in a USB reader. When I tried with a USB stick it worked better.
Here’s some useful commands that might help if you have problems with cloning or backing up your Pi Zero – https://www.binarytides.com/linux-command-check-disk-partitions/
Full Raspbian with Desktop
If you are running the full version of Raspbian then there’s a built in tool you can use to make a copy of your installation. On the menu Accessories > SD Card Copier
Buy Me A Coffee
If you found something useful above please say thanks by buying me a coffee here...
2 Replies to “Back Up Headless Raspberry Pi Zero with RPI-Clone or PiShrink”
Thanks for this article. Your instructions for rpi-clone worked great. The GitHub page for rpi-clone is kind of intimidating, but you showed it’s actually pretty simple. One suggestion for an addition… looks like there’s an option for rpi-clone (`-u`) that allows rpi-clone to run unattended via cron, it would be awesome if there was an example of how to set that up in the article. Thanks again for the pointers.
Great guide. Only thing I needed to look up was argument “status=progress” for the dd command, which shows a progress bar for the .img file creation. Handy for fools like me that installed pi onto a 128gb SD card…