Backup or Restore Raspberry Pi media (SD-cards, USB sticks, SSD drive, ...) to/from compressed image-files right on your Pi!
-
Backup an SD card (or other media) to an image-file.
-
Restore an image-file to an SD card (or other media).
-
Works entirely on a Raspberry Pi (or other linux). No Windows or Mac needed.
-
Creates a compressed image-file that will resize to fill the new media to which it is restored.
-
Supports .img .zip .xz and .gz file formats.
-
Images are compatible with popular imaging software such as Raspberry PI Imager.
-
Supports USB storage devices.
-
Supports backing up the live boot media - but be careful, backing up media that is being written to can result in a courupted backup. You have to turn off "hide root device" in "settings", "options" to enable this.
CAUTION: Reboot first, close everything and don't use the system while it is doing step 1 of backing up! You must send your backup image to a different drive.
-
Supports storing the image files on a mounted network device (ie an SMB server).
-
sudo apt install cifs-utils
-
mkdir shared
-
sudo mount.cifs //192.168.1.18/shared shared -o user=USERNAME,vers=1.0
-
-
Create a library of your own pi images, then restore them to whatever media you want, whenever you want.
-
Can be run from the command line or use the easy menu interface.
-
Should work with most debian and arch distros, see below.
-
See Leepspvideo nice review on youtube: https://www.youtube.com/watch?v=XP6ycUR9Ih0 -- "Very Impressive", "Makes a nice small image", "Really good all-in-one solution"
Paste or type this into a terminal window:
wget https://raw.githubusercontent.com/RichardMidnight/pi-safe/main/pisafe -O pisafe
bash pisafe install
Beta version:
wget https://raw.githubusercontent.com/RichardMidnight/pi-safe/main/pisafe_beta -O pisafe
bash pisafe install
Previous version:
wget https://raw.githubusercontent.com/RichardMidnight/pi-safe/main/pisafe_1.1.1 -O pisafe
bash pisafe install
-
You will need a large "Master" SD-card (eg 32GB), a smaller "Source" SD-card (eg 8GB) and a USB-SD-card reader.
-
Install Raspberry PI OS and PiSafe on an SD card that is large enough to hold some SD image-files. 32GB or 64GB will do. This is your "Master" SD-card.
-
Install Raspberry PI OS or whatever OS you want on the "Source" SD-card. Use as small of an SD-card as you can because the entire card is read in before it is shrunk and compressed. I have been using Sandisk Industrial 8GB cards. Make changes, install other software, change the desktop, whatever you want.
-
To make an image of the "Source" (8GB) card that you can restore anytime in the future do the following...
-
Boot your Pi with your "Master" SD-card as above.
-
Put your "Source" SD-card (8GB) in the USB SD reader and insert it in a Pi USB port.
-
Startup pisafe from the menu. Or in a terminal, type in 'pisafe'
-
Use the menu to select BACKUP, select your SD-card and give the image-file a name.
-
Watch your image-file get created, shrunk and compressed automatically.
Originally developed and tested on Raspberry pi 4 running Raspberry Pi OS Buster.
Also tested on RaspiOS-arm64, Raspberry Pi Desktop, Raspbian Stretch, Ubuntu 20.20 for Rpi, Linux Mint, LMDE.
v1.0.5 Added support for other terminals: lxterminal, gnome-terminal, xfce-terminal, mate-terminal, konsole, xterm, uxterm, qterminal.
V1.0.5 Added support for other text editors: leafpad, mousepad, gedit, kwrite, pluma, featherpad, xed, geany, kate, nano.
V1.1.0 Added support for Manjaro and probably other Arch-based distros.
v1.2.0 Cleaned up the code a lot, added "ignore_freespace_at_end", added erase (fat32, exfat, ntfs, ext4).
PiSafe is optimized for Raspberry Pis running Raspios around the year 2020. It will work with many other linux distributions and other hardware but some features may not be optimized or may not work at all depending on the configuration. The following tips can help you optimize PiSafe for your configuration.
-
Ignore Freespace at end of media
- PiSafe will ignore freespace at the end of the media, speeding up the backup process and using less working space.
- If you have a small amount of data on a large media (eg using 10GB of a 500GB drive), you can resize your partitions (with gparted) and leave freespace (unallocated) at the end of the media which PiSafe will ignore. Note, freespace not at the end of the media cannot be ignored.
-
Shrinking the filesystem on backup
- Shrinking the filesystem is VERY VALUABLE because it creates a smaller image file, and allows you to restore the image to a different size media.
- PiSafe will shrink your filesystem if your main partition is ext4 (and maybe ext3 and ext2) and is the last partition on the media.
- If your install does not default to this partitioning setup you should be able to custom partition the media during your OS installation and put the main ext4 partition at the end. This is needed with standard debian installations including RPD and Linux Mint because they put a swap partition at then end of the media which blocks PiSafe from shrinking the filesystem.
- Alternatively if your main partition is the second to last one and the last one is a swap partition, you may be able to simply delete the swap partition.
- Alternatively you can zero-out your deleted files with bleachbit to optimize compression and then shrink your partitions with gparted.
-
Auto-expand filesystem on restore
- PiSafe will setup the image file to auto-expand to fill the new media on first boot after restoring if your distro supports rc.local.
- If not, you can resize your partition manual by booting from another media and using gparted.
- Note: Auto-expand may not work on an overlay filesystem. Recommend turning off auto-expand in Settings/Options.
-
Data compression
- Compressing the image file with zip, xz, or gz reduces the size of the image file to 1/2 or smaller.
- The default is the industry standard zip.
- The default data compression level is set to 1 out of 9. A higher setting will compress the file a bit more but can take a lot more time.
Thanks to the Raspberry Pi foundation for instructions on how to read and write an image file.
Thanks to https://github.com/Drewsif/PiShrink for the PiShrink engine.
Thanks to all the people who posted code snipets on the web.