To enable OpenWrt to automatically restore a previous backup during the first reboot after flashing, you can integrate the backup file and the restore script into the firmware during the build process. Here’s how to do it:
1. Create the files
Directory
In the root directory of your OpenWrt source tree, create a directory named files
. Any files placed in this directory will be directly copied into the final firmware with the same directory structure.
mkdir -p ~/openwrt/files
2. Add the Backup File and Script
2.1 Place the Backup File
Copy the backup file you’ve prepared (e.g., backup.tar.gz
) into the files/etc/backup/
directory:
mkdir -p ~/openwrt/files/etc/backup
cp /path/to/your/backup.tar.gz ~/openwrt/files/etc/backup/
Replace /path/to/your/backup.tar.gz
with the actual path to your backup file.
2.2 Add the Auto-Restore Script
Create an auto-restore script and place it in the files/etc/init.d/
directory, for example, named restore_backup
:
mkdir -p ~/openwrt/files/etc/init.d
nano ~/openwrt/files/etc/init.d/restore_backup
In the editor, add the following script content:
#!/bin/sh
# Define the backup file path
BACKUP_FILE="/etc/backup/backup.tar.gz"
# Check if the backup file exists
if [ -f "$BACKUP_FILE" ]; then
# Restore the backup
sysupgrade -r $BACKUP_FILE
echo "Backup successfully restored"
# Delete the backup file and the script itself
rm -f $BACKUP_FILE
rm -f /etc/init.d/restore_backup
echo "Backup file and restore script deleted"
else
echo "Backup file not found"
fi
Save and exit the editor.
3. Set Script Permissions and Startup
To ensure the restore script runs on system startup, create a 99_restore_backup
script in the files/etc/uci-defaults/
directory:
mkdir -p ~/openwrt/files/etc/uci-defaults
nano ~/openwrt/files/etc/uci-defaults/99_restore_backup
In this file, add the following:
#!/bin/sh
# Ensure the restore script is executable
chmod +x /etc/init.d/restore_backup
# Enable the restore script to run on startup
/etc/init.d/restore_backup enable
# Delete this initialization script
rm -f /etc/uci-defaults/99_restore_backup
Save and exit the editor.
4. Compile the OpenWrt Firmware
After completing the above steps, compile the OpenWrt firmware. This will generate a firmware image that includes your backup file and the auto-restore script.
5. Flash and Test the Firmware
Flash the generated firmware to your device and verify that the device automatically restores the backup upon the first boot and deletes the script and backup file afterward.
These steps will help you integrate an automatic backup restore feature into your OpenWrt firmware.