Backup only OpenWrt

Hi,

How could I modify this code to reduce the size of what is being backed up?

#!/bin/bash
now=`date +"%d-%m-%y"`
dd if=/dev/mmcblk1 bs=20M| pv | gzip -c > /mnt/sda1/backup_$now.img.gz
#gunzip -c /mnt/yourddimage.img.gz | dd of=/dev/sdX

At the moment the size is nearly 30.0.GB but we know the whole OpenWrt in only a few MB in size. So how can I modify the code to only back up the OpenWrt partitions?

Thanks,

Aaron

Why don't you just backup the config using the regular backup command?

You can then use that to restore your config in the future onto a new/existing device.

In terms of packages. You can create an image using attended sysupgrade, this will include all the packages you currently have installed.

3 Likes

I don't have the attended sysupgrade option available.

You'll need to install the package

opkg install luci-app-attendedsysupgrade

Then in luci goto system > attended sysupgrade. Press search firmware image. press request firmware image. It'll generate an image then there's an option to download image. This will contain all the packages you currently have installed.

If you have this and the tar backup, you'll be able to restore your system.

You can check which files will be backed up by going to system > Backup / Flash Firmware > configuration > open list. If you see files that aren't backed up, you can add them in the text box on the configuration page.

1 Like

Thanks! However, I can't be bothered keeping track of all the packages and having to reinstall them again.

The image generated from attended sysupgrade will include all the packages in the image. You won't need to keep track or manage them manually.

The tar backup will have all the config for your router.

These 2 things combined will be tiny in comparison to the dd backup of the disk.

Only add OpenWrt partitions to the code. :person_shrugging:

But as others may have noted:

Since it's not advised (except on x86_64 as I recall), your use case for the resulting image isn't clear.

So I can backup without having to fill up the sd card because the version of OpenWrt I am using does not allow for resizing.

I didn't make an inquiry, and you agree OpenWrt isn't GBs huge, so your re-noting something about the SD card confused me.

I hope the information about only selecting the relevant partitions and that it's usually not helpful in most targets helps your backup endeavor.

I'm not endorsing the idea of using dd at the block/device level for back-up purposes, but to answer your question based on info from post Losetup not working - #9 by Nightwalker83 where that last block allocated in the partition table is 509951 and from dd --help, you could look into using the count=N argument to dd where N is the last block allocated + 1 (or larger).
I think it might need + 1 since I think the first block is likely 0 and not 1.
You may also need to change the bs=20M parameter if that gets used with the count parameter.
To help this to be accurate in the future, you may want to automate the identification of the count value in case future installations or upgrades changes the partitions.

Something like the following might be worth testing:

#!/bin/bash
now=`date +"%d-%m-%y"`
dd if=/dev/mmcblk1 count=509952 | pv | gzip -c > /mnt/sda1/backup_$now.img.gz
#gunzip -c /mnt/yourddimage.img.gz | dd of=/dev/sdX

As @lleachii wrote, You could also back-up the OpenWrt partitions and I also recommend the partition table.

Something like the following for the partitions assuming as an example that partitions 4, 5 and 6 are identified as OpenWrt:

#!/bin/bash
now=`date +"%d-%m-%y"`
for i in 4 5 6
do
    dd if=/dev/mmcblk1p$i | pv | gzip -c > /mnt/sda1/backup_p$i_$now.img.gz
done

Thanks! I will try that. I just hate manual backup because I nearly always forget to backup something important and something stops working.

There are several methods of generating a list of installed packages (user installed and/or pre-installed on the image) so that you don't have to keep track of them yourself and re-installation can be very simple. Beyond that, though, all of the basic configuration files are included in the backup that is generated by the sysupgrade backup routines (i.e. clicking the backup button in the web interface, or using the CLI equivalent). You can specify additional files/paths to backup as well, and those get included in the tarball.

As has been stated already, creating a bit-for-bit dd copy of your installation is not recommended. In some cases, it can get you back to where you were should you have a storage failure (on a traditional hard drive or SSD, or a flash card), but this can likely cause larger failures if the media itself contains any errors (in particular, never do this for embedded storage). And more importantly, this will only be useful to get you back to the exact version of OpenWrt, and doesn't provide any useful methods of upgrading to new versions since the files are not 'restorable' or even easily readable in this raw state.

3 Likes

What about backing up user files such as this for openvpn, etc? Would that backup the client.ovpn and server.conf?

Yes, provided you direct sysupgrade to include it. ie. my special files to backup:

Summary
## This file contains files and directories that should
## be preserved during an upgrade.

#/etc/example
/etc/openvpn/
/usr/sbin/netifd-netinfo.sh
/www/luci-static/resources/view/status/syslog.js
/root/.config/htop/htoprc
/root/.msmtprc
1 Like

Cool! Thanks.

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