Edit: now also works on ArchLinuxARM for rpi
I am very proud to present my absolute first contribution on github.
With a little help from some members here on the forum I finally got it ready to share.
shrink-backup is a very fast utility for backing up your SBC:s into minimal bootable img files for easy restore with autoexpansion at boot
_ I made this script because I wanted a universal method of backing up my SBC:s into small img files as fast as possible (with rsync), indepentent of what os is in use._
Autoexpansion tested on Raspberry Pi os, Armbian, Manjaro-arm and ArchLinuxARM for rpi with ext4 root partition.
Very fast restore because of minimal size of img file.
Can back up any device as long as root is ext4
Default device that will be backed up is detected by scanning what disk-device root resides on.
This means that if boot is a partition, that partition must be on the same device as root.
Backing up/restoring to/from usb-stick (/dev/sda
) with Raspberry pi os has been tested and works.
Wrinting a sd-card img to a usb-stick and vice versa has also been tested and works.
Ultra-fast incremental backups to existing img files
Don’t forget to make the script executable if you git clone
To restore a backup, simply “burn” the img file to a device using your favorite method.
Usage:
sudo shrink-backup -h
Script for creating an .img file and subsequently keeing it updated (-U), autoexpansion is enabled by default
Directory where .img file is created is automatically excluded in backup
########################################################################
Usage: sudo shrink-backup [-Uatyelh] imagefile.img [extra space (MB)]
-U Update the img file (rsync to existing backup .img), no resizing, -a is disregarded
-a Let resize2fs decide minimum space (extra space is ignored), disabled if using -U
-t Use exclude.txt in same folder as script to set excluded directories
One directory per line: "/dir" or "/dir/*" to only exclude contents
-y Disable prompts in script
-e DO NOT expand filesystem when image is booted
-l Write debug messages in logfile shrink-backup.log in same directory as script
-h --help Show this help snippet
########################################################################
Example: sudo shrink-backup -a /path/to/backup.img
Example: sudo shrink-backup -e -y /path/to/backup.img 1000
Example: sudo shrink-backup -Ut /path/to/backup.img
The folder where the img file is created will ALWAYS be excluded in the backup.
If -t
option is selected, exclude.txt MUST exist (but can be empty) within the directory where the script is located or the script will exit with an error.
Use one directory per line in exclude.txt.
/directory/*
= create directory but exclude content.
/directory
= exclude the directory completely.
If -t
is NOT selected the following folders will be excluded:
/lost+found
/proc/*
/sys/*
/dev/*
/tmp/*
/run/*
/mnt/*
/media/*
/var/log.hdd
/var/swap
Rsync WILL cross filesystem boundries, so make sure you exclude external drives unless you want them included in the backup.
Use -l
to write debug info into shrink-backup.log
file in the same directory as the script.
Applications used in the script:
- fdisk (sfdisk)
- dd
- parted
- e2fsck
- truncate
- mkfs.ext4
- rsync
Info
Theoretically the script should work on any device with maximum 2 partitions (boot and root).
The script can handle maximum 2 partitions, if there are more than that on root device the script will fail with an error.
Even if you forget to disable autoexpansion on a non supported system, the backup will not fail.
Order of operations - image creation
- Reads the block sizes of the partitions
- Uses
dd
to create the boot part of the system + a few megabytes to include the filesystem on root (this can be a partition) - Removes and recreates the root partition, size depends on options used when starting the script
- Creates a new ext4 filesystem with the same UUID and LABEL as the system you are backing up from
- Uses
rsync
to sync both partitions (if more than one)
Added space is added on top of df
reported “used space”, not the size of the partition. Added space is in MB, so if you want to add 1GB, add 1024.
The script can be instructed to set the img size by requesting recomended minimum size from e2fsck
by using the -a
option.
This is not the absolute smallest size you can achieve bit is the “safest” way to create a “smallest possible” img file.
If you do not increase the size of the filesystem you are backing up too much, you can most likely keep it updated with the update function (-U
) of the script.
Smallest possible image
To get the absolute smallest img file possible, do NOT set -a
option and set “extra space” to 0
Example: sudo shrink-backup /path/to/backup.img 0
This will instruct the script to get the used space from df
and adding 192MB “wiggle room”.
If you are like me, doing a lot of testing, rewriting the sd-card multiple times. The extra time it takes each time will add up pretty fast.
Example:
-rw-r--r-- 1 root root 3.7G Jul 22 21:27 test.img # file created with -a
-rw-r--r-- 1 root root 3.3G Jul 22 22:37 test0.img # file created with 0
Disclaimer:
Because of how filesystems work, df
is never a true representation of what will actually fit on a created img file.
Each file, no matter the size, will take up one block of the filesystem, so if you have a LOT of very small files (running docker f.ex) the “0 added space method” might fail during rsync. Increase the 0 a little bit and retry.
This also means you have VERY little free space on the img file after creation.
If the filesystem you back up from increases in size, an update (-U
) of the img file might fail.
Order or operations - image update
- Probes the img file for information about partitions
- Mounts root partition with an offset for the loop
- Checks if multiple partitions exists, if true, loops the boot with an offset and mounts it within the root mount
- Uses
rsync
to sync both partitions (if more than one)
To update an existing img file simply use the -U
option and the path to the img file.
Changing size in an update is not possible at the moment but is in the todo list for the future.
Thank you for using my software <3
A backup is not really a backup until it has been restored.