Make full sd card image backup of running OpenWrt

While /dev/mmcblk0 gives you access to the whole SD card this is usually not what is actually mounted as a file system. E.g. have a look at the output of the mount command (or lsblk for that matter).

I think OpenWrt on a Pi 4 has two partitions: boot (/dev/mmcblk0p1) and root (/dev/root).

When I try to mount the root partition as readonly it says device is busy?

Do I need to reboot the Pi with boot and root set to readonly using /etc/fstab maybe? How else can I get /root as readonly?

Flex

Hi, the savest way to go is creating a backup of the card with a cardreader while running another os.

Uwe

Why? What's so dangerous about mounting the root and boot partitions as read only and then running a dd command? Does anyone know?

In this thread someone appears to describe making a backup with dd on a read-only filesystem. This post talks about how to make OpenWrt boot up with /boot and /root in read only mode.

I would ideally like to run a script in a crontab that will force OpenWrt to reboot and put /boot and /root into readonly mode... then run a dd command something like this:
# dd if=/dev/mmcblk0 conv=sync,noerror bs=128K | gzip -c | ssh root@my-other-server dd of=openWrt-backup.gz

And then return things to read/write again... something like this:

mount -o rw,remount /dev/mmcblk0p1
mount -o rw,remount /dev/root

Wouldn't that be a very useful script to have.... to automate backups so I don't have to do everything manually and loose my internet connection? Ideally I would like a complete image made of the whole sd card so I don't have to mess around with installing things all over again.

Any advice and help gratefully appreciated.

Flex

I believe you might still be cramped up in the PC world used to taking clones of hard drives. In the Embedded space doing any such does not make much sense. The main "image" and its "configuration" should really be nicely separated so taking a backup of the configuration should be more than sufficient. If you "manipulate" your system in any other way then that is considered a bad thing and you might want to consider upstreaming such changes if they are truly useful in order for them to get into the next "official" image. Or, of course, build your own custom image if you truly have such a special use case mandating that.

1 Like

Is there any particular reason you are looking to make what sounds like daily backups of your entire configuration? In most cases, not a lot (if anything at all) is changing on the main OpenWrt related partition(s) except for when the user actively installs a package and/or changes configuration files. This is true unless you are also using the device as something of a NAS or server for other purposes -- in which case, you might want to consider writing to a different partition/device instead, which will make it easier to backup/restore if needed.

OpenWrt has a handful of tools already that should typically accomplish the task of backing up and restoring your configuration.

  • There is a system configuration backup (and the files/locations can be customized if you wish to have backups containing more than the typical core files).
  • There is the opkg package manager that makes it pretty easy to install packages. If you have a lot of stuff installed a quick script can make this fast and trouble free to reinstall if you start over with a fresh OpenWrt image (the script can be as simple as keeping a list of your installed packages in the following format below):
opkg update
opkg install <package1> <package2> ... <package n>
  • There are user developed scripts such as this one that can catalog and reinstall user added packages.
  • You can also simply build your own images using the image builder so that you have a custom image with everything pre-installed for your needs.
  • or use the image builder server that another user developed (which is pretty cool!)
  • Or pop the card out and duplicate it on a desktop class computer (to another card, a simple folder or compressed file, or an image format that can be written back later) when you have your device configured as you want, and take a standard backup of your config files when you make subsequent minor changes.

If I'm missing a key reason you wish to do daily backups of the whole system, please add some detail so that I/we can understand and help you come up with a better solution.

1 Like

@sumo and @psherman

Thank you both for your input. I accept that there are other ways to achieve my goal. But none of the options given so far are an easy and automatic solution. That's what I am after.

I looked at the script that can catalog and reinstall user added packages, which seems like a good idea, but it doesn't work on my OpenWrt. I get:

ls: /overlay/usr/lib/opkg/info/*.control: No such file or directory
No user packages found

Likewise I don't want to have to keep track of everything I ever installed by myself. I'll forget things.

I made a full SD card backup already in which I took the SD card out but I don't want to have to keep doing that because it's a pain and I loose internet when I do.

I don't need something as elaborate as building my own image.

Here is what I have got working so far:

Anytime I tried to remount rootfs as readonly I got a "Failed - resource busy" so by following a hint from here I edited /lib/preinit/80_mount_root and commented out # mount_root (line 19) and then I edited /lib/preinit/79_move_config and changed...

mount -t vfat -o rw,noatime $BOOTPART /boot
To
mount -t vfat -o ro,noatime $BOOTPART /boot

Then I rebooted OpenWrt and indeed the boot and root fs were then readonly.

Then running this command to image the first 800MB of the SD card starting from /boot

# dd if=/dev/mmcblk0 bs=4M count=200 | gzip > /mnt/usb/openWrt-backup.img.gz

openWrt-backup.img.gz is 37MB zipped or 820MB when unzipped.

I can make /boot and /root rw again like this:
# mount -o remount,rw /boot
# mount -o remount,rw /dev/root

But the problem is, although I CAN SSH in to OpenWrt… I get no internet connectivity:
image
So I after editing those /lib/preinit files back to how they were I had to reboot a second time.

Now I checked the .img file with losetup and I get this:

The boot partition mounts fine but the root does not. I'm guessing that's because my dd command.. I didn't dd the whole sd card. I was hoping I wouldn't have to. It's a 32GB card but only about 500MB is being used!

My questions:

  1. If I run the dd command on my sd card say, once a week, would that potentially cause the SD card to fail?
  2. Is it possible to make /dev/root readonly some way other than what I did above so I don't have to reboot?
  3. If no then is it possible to make the rootfs mount as read only some other way so I can get it back to read/write and normal OpenWrt functioning without having to reboot a second time?
  4. Here is the partition info on my OpenWrt (on a Pi 4)
    Any hint on what dd command I need to image the whole SD card? If I do this then I will just get the /boot partition right? # dd if=/dev/mmcblk0 bs=4M | gzip > /mnt/usb/openWrt-backup.img.gz
  5. I think I will switch to a smaller card.. e.g: 4 or 8GB.

Thank you!

Flex

A smaller card would make a lot of sense if you're going to use DD or other direct imaging techniques. But how often are you really modifying your config (installing packages and/or changing the configuration)?

Another option -- would rsync work for your needs? I have never tried to use an rsync backup on OpenWrt, but it would be an interesting experiment -- send the files to an rsync server and then move them to a properly formatted SD card and see if it will boot.

@psherman

Cheers. Well if dd can do the job and not damage my sd card then I would use it instead of rsync.

To anyone:
Any advice about my questions above 1 - 4 much appreciated?

Thank you,

Flex

Hi

I am using RPI2 at the moment as my main router (my VDSL goes up to 70mbit only , so RPI2 is enough for now).

My image backup script backs up to local smb share on other computer

#!/bin/sh

mkdir -p /mnt/myshare
mount -t cifs //<server ip>/yourshare /mnt/myshare -o rw,username=xxx,password=xxx,vers=2.1

sync

dd if=/dev/mmcblk0 bs=4M count=75 | pv > /mnt/myshare/misc/backups/openwrt/images/rpi2/openwrt_$(date +"%Y%m%d%H%M%S").img

umount /mnt/myshare

I placed it on /etc/config/imageBackup.sh

And created a cron job to execute it twice a week

45 0 * * 1,4 /etc/config/imagebackup.sh

Do note my script backs up only the first 300mb of the sd card , since at least here the data partition is 256MB.

lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
mmcblk0     179:0    0   15G  0 disk
├─mmcblk0p1 179:1    0   20M  0 part /boot
└─mmcblk0p2 179:2    0  256M  0 part /

So regardless of your sd card size ,only first 300mb are getting backed up.

This way i have a full image backup of my setup and you can test it easily by writing the dd output to a new sd card and put it in your RPI.

I assume you are new to OpenWRT , so do note , when upgrading to new OpenWRT the image backup wont be much of an help, you'll need to reinstall your packages manually or via a script.

@hagayg
Thanks so much for your very helpful response.

Do you make the /boot and /root partitions readonly before running that script or do you leave them as read/write? I was looking at your other thread... does this command make the /boot and /root partitions readonly?

mount -o remount,ro /overlay

I don't see that command in your script?

Cheers,

Flex

I am using the script i attached on earlier post , so no remounting as read only.

Boot partition is RPI standard and not really related to OpenWRT , mainly for booting the device.

Unless you have some sort of "file data sensitive" application on your OpenWRT device i believe no remounting is needed , from my experience never had problems booting my backed up images.

1 Like

Can you please explain how you write the dd output to a new sd card? Because I tried this command..

# dd if=/dev/mmcblk0 bs=4M count=200 | gzip > /mnt/usb/openWrt-backup.img.gz

two times now. As you can see above when I use the # losetup command it sees two partitions /boot and / (root). I can mount the /boot but when I try to mount the root partition I get errors.

This evening I tried that command again... my source sd card is 32GB.. I used Balena etcher to copy the backup .img file of that sd card (limited to count=200 as above) to a 4GB SD card. The image seemed to write ok to the 4GB sd card but then I could not open that sd card in GParted. I think it just couldn't understand the dimensions of the SD card and why I was trying to put a piece of a 32GB card onto a 4GB card??

Is there some more complicated series of commands you need to run to prepare the new destination sd card to accept the "piece" image from the source card in OpenWrt? E.g: Create a new MBR and define limits of each partition on the new sd card?

Cheers,

Flex

Hi,

Not much of a Linux "expert" , so normally i am using win32 disk writer on my windows machine to write the file to a sd card (for all my RPI's).

But i can see you are gziping your DD output , so you cant use the gzipped file as input to your DD. (it would indeed cause a bad partition layout)

Also you are reading 800mb from your sd card , so as i mentioned, unless your data partition is bigger than 256mb , its pointless.

I would suggest for your sanity , to drop gzipping for start and then write the output to a new sd card , it should be o.k.

If it goes well there , try with gzip with correct "piping order".

As for 32gb or 4gb sdcard , all should work , since default partition data is less then 300mb.

@hagayg I understand what you mean now!

Sounds like you flash OpenWrt on to an sd card and leave the partitions as they are? When I flash the latest Pi 4 snapshot to a 4GB sd card I get this partition layout:

The root partition is tiny! Do you increase its size? (/dev/sdb2 here)

If I increase it to around 1GB then I found this command works for me:
dd if=/dev/mmcblk0 bs=4M count=300 | pv | gzip > /mnt/usb/openwrt_$(date +"%Y%m%d%H%M%S").img.gz

  1. OpenWrt can still be running.
  2. The dd command makes an image and saves it to an attached usb key.
  3. The image starts from the start of the sd card and extends a little beyond the limit of the partition containing the root filesystem... so the image contains both /boot and /dev/root.
  4. As you pointed out, an advantage of using Win32 Disk Imager (at least on windows) is this image can then be flashed to any other sd card even if much smaller in size. By selecting "Read Only Allocated Partitions" it ignores any unallocated/unpartitioned space on the backup image.

I think this is an efficient way of making a full sd card image that could go in a crontab and run once a while for emergencies, maybe adding to a backup list where the oldest is then deleted.

I understand another approach is needed when it is time to update to the latest firmware.

Now I wish I had a script that could shrink a backup image and when flashing an image to auto-inflate the root partition to occupy all available space on the sd card!

Cheers,

Flex

1 Like

Now I wish I had a script that could shrink a backup image and when flashing an image to auto-inflate the root partition to occupy all available space on the sd card!

The later may be achieved by resize2fs. However, I believe it can't go beyond the partition size one initially configured.

BTW: Have you seen that thread? Talks about A/B images and backup.

My partition sizes are the ones came with the official ext4 image.

RPI 4 still does not have an official release , so maybe they set other partition sizes.

Tough 100mb should be enough for most uses.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.