Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revisionBoth sides next revision
docs:guide-user:installation:generic.backup [2018/08/06 00:16] – [Create Full MTD Backup from router] oslyakdocs:guide-user:installation:generic.backup [2022/07/11 15:06] – Add instructions for how to explore MTD backups spookyuser
Line 1: Line 1:
-====== Generic Backup ======+====== Generic NOR backup ====== 
 +  * This guide describes how to perform block-level backup/restore via [[docs:guide-quick-start:sshadministration|CLI]]. 
 +  * Follow [[docs:guide-user:troubleshooting:backup_restore|Backup and restore]] for file-level backup/restore.
  
-===== Backup system files =====+<WRAP center round important 60%> 
 +NAND-based devices should use [[docs:techref:flash#nand-specific_tools_for_reading_and_writing_to_raw_nand|NAND-aware utilities]], as ''dd'' does not properly handle the error correction or bad-block marking of NAND flash. 
 +</WRAP>
  
-Please have a look at [[docs:techref:file_system]] and the [[docs:techref:flash.layout#Details|flash layout details]] and take notice, that OpenWrt covers only the ''firmware'' part. The **[[docs:techref:bootloader]] partition**, ''ART''/''NVRAM'' and similar partitions are NOT part of the OpenWrt firmware. If something should go wrong and the data on these partition gets unexpectedly corrupted, you will not be able to replace it via public OpenWrt sources!+Please have a look at [[docs:techref:file_system]] and the [[docs:techref:flash.layout#Details|flash layout details]] and take notice, that OpenWrt covers only the ''firmware'' part. 
 +The **[[docs:techref:bootloader]] partition**, ''ART''/''NVRAM'' and similar partitions are NOT part of the OpenWrt firmware. 
 +If something should go wrong and the data on these partition gets unexpectedly corrupted, you will not be able to replace it via public OpenWrt sources!
  
 Since OpenWrt does not write to those partitions, it is very unlikely that they get corrupted by OpenWrt itself. Since OpenWrt does not write to those partitions, it is very unlikely that they get corrupted by OpenWrt itself.
Line 9: Line 15:
 Otherwise, in case you lose that data, you would have to go to the forum, ask somebody to make a backup of his and send it to you, then you would have to replace the MAC address, and then flash it via [[docs:techref:hardware:port.jtag]], since your device would probably not boot any longer. Otherwise, in case you lose that data, you would have to go to the forum, ask somebody to make a backup of his and send it to you, then you would have to replace the MAC address, and then flash it via [[docs:techref:hardware:port.jtag]], since your device would probably not boot any longer.
  
-==== Create ART backup ====+===== Create ART backup ====
 +If your ART-partition got corrupted, you would still be able to boot OpenWrt and only your wireless would not function correctly any longer. 
 +Easy fix with ''[[docs:techref:mtd]]''.
  
-If your ART-partition got corrupted, you would still be able to boot OpenWrt and only your wireless would not function correctly any longer. Easy fix with ''[[docs:techref:mtd]]''+<code bash
- +dd if=/dev/$(sed -n -e '/:.*"art"/s///p' /proc/mtd) of=/tmp/art.backup
-<code> +
-dd if=/dev/$(grep -'"art"' /proc/mtd |cut -c 1-4) of=/tmp/art.backup+
 </code> </code>
- 
  
 If your bootloader-partition got corrupted, you would not even have a [[docs:techref:bootloader#additional_functions|booloader console]] which you could only access through [[docs:techref:hardware:port.serial|Serial Port]] any longer, and the only way to recover from this would be though the [[docs:techref:hardware:port.jtag|JTAG Port]] or by de-soldering the flash-chip, but please see [[docs:guide-user:troubleshooting:generic.debrick]] for help. If your bootloader-partition got corrupted, you would not even have a [[docs:techref:bootloader#additional_functions|booloader console]] which you could only access through [[docs:techref:hardware:port.serial|Serial Port]] any longer, and the only way to recover from this would be though the [[docs:techref:hardware:port.jtag|JTAG Port]] or by de-soldering the flash-chip, but please see [[docs:guide-user:troubleshooting:generic.debrick]] for help.
  
- +However, once you've gotten yourself into the position to write to the flash again, you will still need something you can write to it. 
-However, once you've gotten yourself into the position to write to the flash again, you will still need something you can write to it. Something that will work. And here is, where your backup will come in handy: +Something that will work. 
- +And here is, where your backup will come in handy: 
-<code>+<code bash>
 dd if=/dev/mtd0 of=/tmp/boot.backup dd if=/dev/mtd0 of=/tmp/boot.backup
 </code> </code>
Line 29: Line 34:
 Then copy your backup-file via scp or ssh to your PC and keep them safe for the time when you may need them. Then copy your backup-file via scp or ssh to your PC and keep them safe for the time when you may need them.
  
-==== Create Full MTD Backup ==== +===== Create full MTD backup ===== 
- +This script assumes a working Bash and SSH in native Unix-like or WSL environment. 
-This script assumes a working bash and SSH on the host (4.0 or newer for ControlMaster support). If you've changed your router's IP address, change the "OPENWRT variable value to the hostname/IP of your OpenWRT router+If you've changed your router's IP address, change the OPENWRT variable value to the hostname/IP of your OpenWrt router. 
- +This will backup your mtd contents to a compressed tarball file ''mtd_backup.tgz'' in the same folder as the script.
-On your local PC (not on the router), using a bash terminal (not Windows Command Prompt, Windows 10 Linux subsystem bash terminal is fine) create a new file (eg. ~/mtdbk.sh) containing the below script. +
-If running on windows, make sure to save the text file as UNIX (LF) encoded, otherwise you'll get syntax errors, and then execute it (./mtdbk.sh). +
-If running on unix-like system, give this file execution privileges (chmod u+x mtdbk.sh) and then execute it (./mtdbk.sh)+
-This will backup your mtd contents to a compressed tarball file "mtd_backup.tgzin the same folder as the script. Make sure to use bash. (Don't do sh ./mtdbk.sh)+
  
-<code>+<code bash> 
 +cat << "EOF" > mtdbk.sh
 #!/bin/bash #!/bin/bash
  
Line 44: Line 46:
  
 function die() { function die() {
- echo "$@"+ echo "${@}"
  exit 2  exit 2
 } }
  
 OUTPUT_FILE="mtd_backup.tgz" OUTPUT_FILE="mtd_backup.tgz"
- +OPENWRT="root@openwrt.lan"
-OPENWRT="root@192.168.1.1" +
 TMPDIR=$(mktemp -d) TMPDIR=$(mktemp -d)
-BACKUP_DIR="$TMPDIR/mtd_backup" +BACKUP_DIR="${TMPDIR}/mtd_backup" 
-mkdir -p "$BACKUP_DIR" +mkdir -p "${BACKUP_DIR}
-SSH_CONTROL="$TMPDIR/ssh_control"+SSH_CONTROL="${TMPDIR}/ssh_control"
  
 function cleanup() { function cleanup() {
Line 64: Line 64:
  
  echo "Removing temporary backup files"  echo "Removing temporary backup files"
- rm -r "$TMPDIR"+ rm -r "${TMPDIR}"
 } }
 trap cleanup EXIT trap cleanup EXIT
Line 70: Line 70:
 # Open master ssh connection, to avoid the need to authenticate multiple times # Open master ssh connection, to avoid the need to authenticate multiple times
 echo "Opening master SSH connection" echo "Opening master SSH connection"
-ssh -oControlMaster=yes -oControlPath="$SSH_CONTROL" -o ControlPersist=10 -n -N "$OPENWRT"+ssh -o "ControlMaster=yes-"ControlPath=${SSH_CONTROL}" -o "ControlPersist=10-n -N "${OPENWRT}"
  
 # This is the command we'll use to reuse the master connection # This is the command we'll use to reuse the master connection
-SSH_CMD=(ssh -oControlMaster=no -oControlPath="$SSH_CONTROL" -n "$OPENWRT")+SSH_CMD=(ssh -o "ControlMaster=no-"ControlPath=${SSH_CONTROL}" -n "${OPENWRT}")
  
 # List remote mtd devices from /proc/mtd. The first line is just a table # List remote mtd devices from /proc/mtd. The first line is just a table
 # header, so skip it (using tail) # header, so skip it (using tail)
 "${SSH_CMD[@]}" 'cat /proc/mtd' | tail -n+2 | while read; do "${SSH_CMD[@]}" 'cat /proc/mtd' | tail -n+2 | while read; do
- MTD_DEV=$(echo $REPLY | cut -f1 -d:) + MTD_DEV=$(echo ${REPLY| cut -f1 -d:) 
- MTD_NAME=$(echo $REPLY | cut -f2 -d\"+ MTD_NAME=$(echo ${REPLY| cut -f2 -d\"
- echo "Backing up $MTD_DEV ($MTD_NAME)"+ echo "Backing up ${MTD_DEV(${MTD_NAME})"
  # It's important that the remote command only prints the actual file  # It's important that the remote command only prints the actual file
  # contents to stdout, otherwise our backup files will be corrupted. Other  # contents to stdout, otherwise our backup files will be corrupted. Other
Line 89: Line 89:
  
 # Use gzip and tar to compress the backup files # Use gzip and tar to compress the backup files
-echo "Compressing backup files to \"$OUTPUT_FILE\"" +echo "Compressing backup files to \"${OUTPUT_FILE}\"" 
-(cd "$TMPDIR" && tar czf - "$(basename "$BACKUP_DIR")") > "$OUTPUT_FILE" || die 'tar failed, aborting...'+(cd "${TMPDIR}" && tar czf - "$(basename "${BACKUP_DIR}")") > "${OUTPUT_FILE}" || die 'tar failed, aborting...'
  
 # Clean up a little earlier, so the completion message is the last thing the user sees # Clean up a little earlier, so the completion message is the last thing the user sees
Line 97: Line 97:
 trap EXIT trap EXIT
  
-echo -e "\nMTD backup complete. Extract the files using:\ntar xzf \"$OUTPUT_FILE\""+echo -e "\nMTD backup complete. Extract the files using:\ntar xzf \"${OUTPUT_FILE}\"" 
 +EOF 
 +chmod +x mtdbk.sh 
 +./mtdbk.sh
 </code> </code>
  
-==== Create Full MTD Backup from router ==== +===== Create full MTD backup from OpenWrt =====
 The method above works great, but only if you have SSH root access to you router. The method above works great, but only if you have SSH root access to you router.
 In some cases when you don't have SSH root access to router, but can connected from UART console. In some cases when you don't have SSH root access to router, but can connected from UART console.
-For example TP-Link Archer C9 HW ver 5.0 with original stock firmware +For example TP-Link Archer C9 HW ver 5.0 with original stock firmware.
 You can make backup from router to you host. You can make backup from router to you host.
 +<code bash>
 +# Save the script
 +cat << "EOF" > /tmp/backup.sh
 +#!/bin/sh
  
-<code> +BACKUP_HOST="pc.lan
-cd /tmp +BACKUP_USER="root" 
-vi ./backup.sh +echo "Backup host ${BACKUP_HOST}"
-</code> +
-Press "i" key for insert, copy paste the code bellow, press "ESC" and enter ":wq" (without quotes) +
- +
-<code> +
-#!/bin/ash +
- +
-BACKUP_DIR=/tmp +
-BACKUP_HOST="192.168.0.66+
-BACKUP_USER=root +
-echo "Backup host $BACKUP_HOST"+
  
 cat /proc/mtd | tail -n+2 | while read; do cat /proc/mtd | tail -n+2 | while read; do
-  MTD_DEV=$(echo $REPLY | cut -f1 -d:) +  MTD_DEV=$(echo ${REPLY| cut -f1 -d:) 
-  MTD_NAME=$(echo $REPLY | cut -f2 -d\"+  MTD_NAME=$(echo ${REPLY| cut -f2 -d\"
-  echo "Backing up $MTD_DEV ($MTD_NAME)" +  echo "Backing up ${MTD_DEV(${MTD_NAME})" 
-  dd if=/dev/${MTD_DEV}ro | ssh $BACKUP_USER@$BACKUP_HOST "dd of=~/${MTD_DEV}_${MTD_NAME}.backup"+  dd if=/dev/${MTD_DEV}ro | ssh -y ${BACKUP_USER}@${BACKUP_HOST"dd of=~/${MTD_DEV}_${MTD_NAME}.backup"
 done done
-</code>+EOF
  
-and run this script +# Run the script 
- +sh /tmp/backup.sh
-<code> +
-chmod 777 ./backup.sh +
-./backup.sh+
 </code> </code>
  
 +Now you'll be asked for SSH password from PC for each mtd device 5 or more times.
  
 +After operation completed, you'll find all files in user home directory on PC.
 +
 +If you on linux and wish to see progress you can run in separate console on PC:
 +<code bash>
 +watch -n 0.2 ls -l --block-size=K ~
 +</code>
  
-==== Create Backup from Bootloader ====+===== Create backup from bootloader ====
 +Sometimes it might be necessary to backup settings/partitions from original firmware. 
 +Depending on the bootloader, different strategies might be possible.
  
-Sometimes it might be necessary to backup settings/partitions from original firmware. Depending on the bootloader, different strategies might be possible.+The flash-chip is mapped to a start address. 
 +With uboot it should be in the following settings:
  
-The flash-chip is mapped to a start adress. With uboot it should be in settings +<code bash>
-<code>+
 printenv printenv
 bdinfo bdinfo
 </code> </code>
  
-  * memory dump to serial that is logged (uboot: //md// ; redboot: //dump//) +Memory dump to serial that is logged (uboot: //md// ; redboot: //dump//). 
-  * writing dumps to tftp or nfs+Writing dumps to tftp or nfs.
  
-==== Restore Backup ====+===== Restore backup from bootloader console ===== 
 +Many bootloader allow you to work with mtd partition, but beware: they do not have to be identical with the Kernel mtd partitions! 
 +Also, with some bootloaders, you cannot use mtd-partition, you must work with offsets. 
 +In the latter case, it is probably a good idea to write down these correct offsets when you make the backups.
  
-=== from Bootloader console === +===== Restore backup from OpenWrt console ===== 
-Many bootloader allow you to work with mtd partition, but beware: they do not have to be identical with the Kernel mtd partitions! Also, with some bootloaders, you cannot use mtd-partition, you must work with offsets. In the latter case, it is probably a good idea to write down these correct offsets when you make the backups. +<code bash> 
- +mtd write art.backup art 
- +</code> 
-=== from OpenWrt console === +Above method could work, but most probably will not as art partition is usually not writable, so you will have to compile you own kernel after doing some [[toh:tp-link:tl-wr1043nd#making_bootloader_partition_writable|minor modification]].
-<code>mtd write art.backup art</code> +
-Above method could work, but most probably will not as art partition is usually not writable, so you will have to compile you own kernel after doing some minor modification like described here: +
-[[toh/tp-link/tl-wr1043nd#making.readonly.mtd.partition.writable|TL-WR1043ND]] +
-or here: [[https://forum.openwrt.org/viewtopic.php?pid=135278#p135278]].+
 Then you must flash this to you device, boot it, and now the partition should be writable. Then you must flash this to you device, boot it, and now the partition should be writable.
  
-From: +===== Exploring MTD Backups ===== 
-[[https://forum.openwrt.org/viewtopic.php?pid=134950#p134950]] +If you want to explore the contents of an MTD backup on your computer, without restoring it to openwrt, you can use [[https://github.com/sviehb/jefferson|jefferson]] with the command
-[[https://forum.openwrt.org/viewtopic.php?pid=99089#p99089]]+
  
-===== Backup OpenWrt configuration ===== +<code bash
- +jefferson mtd4_rootfs_data.backup -d rootfs
-You can backup your OpenWrt configuration through the web interface via //System > Backup / Flash Firmware > Generate Archive//. If the web interface is not available, connect using SSH and enter: +
-<code> +
-sysupgrade --create-backup /tmp/backup-`cat /proc/sys/kernel/hostname`-`date +%F`.tar.gz; ls /tmp/backup*+
 </code> </code>
  
-Now on your local computer, use scp to retreive the file+Where the mtd4_rootfs_data.backup file is the mtd block you want to exploreLikely this will be >rootfs_data as it contains most of the customizations you will have made to openwrt.
-(Remember to use the ip of your OpenWRT device and the filename of your backup file.+
- +
-<code> +
-scp root@192.168.1.1:/tmp/backup-openwrt-2014-08-26.tar.gz . +
-</code> +
- +
-You might also want to backup other files you have edited such as+
- +
-   * ''/etc/ethers'' +
-   * ''/home'' (if you added users) +
-   * ''/etc/config/firewall.user'' +
- +
-see also ''[[docs:guide-user:base-system:notuci.config#etcsysupgradeconf|/etc/sysupgrade.conf]]'' and [[docs:guide-user:installation:generic.sysupgrade#ensure_desired_configuration_files_will_be_saved]] +
- +
-===== Restore OpenWrt configuration ===== +
- +
-You can restore the previously saved OpenWrt configuration via +
-  * web interface: //System > Backup / Flash Firmware > Backup / Restore// +
-  * command line:\\ <code> tar xzvf /path/to/your/backup-xxx.tar.gz -C / </code> +
-  * If you got a .tar backup file and get "invalid magic" error (upgrade tar to latest version first):\\ <code> +
- cd / +
- tar -xvf /path/to/your/backup-xxx.tar</code>+
  • Last modified: 2023/03/19 10:23
  • by ryanc