User Tools

Site Tools


doc:howto:generic.backup

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
doc:howto:generic.backup [2013/07/03 10:57]
lorema
doc:howto:generic.backup [2016/04/01 22:15] (current)
benba [Create Full MTD Backup]
Line 1: Line 1:
 +====== Generic Backup ======
 +
 +===== Backup system files =====
 +
 +Please have a look at [[doc:​techref:​file_system]] and the [[doc:​techref:​flash.layout#​Details|flash layout details]] and take notice, that OpenWrt covers only the ''​firmware''​ part. The **[[doc:​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.
 +However, if you mess around with the bootloader, you certainly should create a copy of this data on your PC.
 +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 [[doc:​hardware:​port.JTAG]],​ since your device would probably not boot any longer.
 +
 +==== 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 ''​[[doc:​techref:​mtd]]''​.
 +
 +<​code>​
 +dd if=/​dev/​$(grep -i '"​art"'​ /proc/mtd |cut -c 1-4) of=/​tmp/​art.backup
 +</​code>​
 +
 +
 +If your bootloader-partition got corrupted, you would not even have a [[doc/​techref/​bootloader#​additional.functions|booloader console]] which you could only access through [[doc:​hardware:​port.Serial|Serial Port]] any longer, and the only way to recover from this would be though the [[doc:​hardware:​port.JTAG|JTAG Port]] or by de-soldering the flash-chip, but please see [[doc:​howto:​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. Something that will work. And here is, where your backup will come in handy:
 +
 +<​code>​
 +dd if=/​dev/​mtd0 of=/​tmp/​boot.backup
 +</​code>​
 +
 +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 ====
 +
 +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.
 +
 +On your local PC (not on the router), using a bash terminal (not Windows Command Prompt) create a new file (eg. ~/mtdbk.sh) containing the below script. Give this file execute 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.tgz"​ in the same folder as the script.
 +
 +<​del>​not working; Line 5: Syntax error: unexpected "​("​ FIXME--- //Korpi 2016/02/12 21:48// with ArduinoYún</​del> ​ --- //benba 2016/04/01 22:13// works fine for me
 +<​code>​
 +#!/bin/bash
 +
 +set -e
 +
 +function die() {
 + echo "​$@"​
 + exit 2
 +}
 +
 +OUTPUT_FILE="​mtd_backup.tgz"​
 +
 +OPENWRT="​root@192.168.1.1"​
 +
 +TMPDIR=$(mktemp -d)
 +BACKUP_DIR="​$TMPDIR/​mtd_backup"​
 +mkdir -p "​$BACKUP_DIR"​
 +SSH_CONTROL="​$TMPDIR/​ssh_control"​
 +
 +function cleanup() {
 + set +e
 +
 + echo "​Closing master SSH connection"​
 + "​${SSH_CMD[@]}"​ -O stop
 +
 + echo "​Removing temporary backup files"
 + rm -r "​$TMPDIR"​
 +}
 +trap cleanup EXIT
 +
 +# Open master ssh connection, to avoid the need to authenticate multiple times
 +echo "​Opening master SSH connection"​
 +ssh -oControlMaster=yes -oControlPath="​$SSH_CONTROL"​ -o ControlPersist=10 -n -N "​$OPENWRT"​
 +
 +# This is the command we'll use to reuse the master connection
 +SSH_CMD=(ssh -oControlMaster=no -oControlPath="​$SSH_CONTROL"​ -n "​$OPENWRT"​)
 +
 +# List remote mtd devices from /proc/mtd. The first line is just a table
 +# header, so skip it (using tail)
 +"​${SSH_CMD[@]}"​ 'cat /​proc/​mtd'​ | tail -n+2 | while read; do
 + MTD_DEV=$(echo $REPLY | cut -f1 -d:)
 + MTD_NAME=$(echo $REPLY | cut -f2 -d\")
 + echo "​Backing up $MTD_DEV ($MTD_NAME)"​
 + # It's important that the remote command only prints the actual file
 + # contents to stdout, otherwise our backup files will be corrupted. Other
 + # info must be printed to stderr instead. Luckily, this is how the dd
 + # command already behaves by default, so no additional flags are needed.
 + "​${SSH_CMD[@]}"​ "dd if='/​dev/​${MTD_DEV}ro'"​ > "​${BACKUP_DIR}/​${MTD_DEV}_${MTD_NAME}.backup"​ || die "dd failed, aborting..."​
 +done
 +
 +# Use gzip and tar to compress the backup files
 +echo "​Compressing backup files to \"​$OUTPUT_FILE\""​
 +(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
 +cleanup
 +# Reset signal handler
 +trap EXIT
 +
 +echo -e "\nMTD backup complete. Extract the files using:\ntar xzf \"​$OUTPUT_FILE\""​
 +</​code>​
 +
 +==== 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.
 +
 +The flash-chip is mapped to a start adress. With uboot it should be in settings
 +<​code>​
 +printenv
 +bdinfo
 +</​code>​
 +
 +  * memory dump to serial that is logged (uboot: //md// ; redboot: //dump//)
 +  * writing dumps to tftp or nfs
 +
 +==== 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 OpenWrt console ===
 +<​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.
 +
 +From:
 +[[https://​forum.openwrt.org/​viewtopic.php?​pid=134950#​p134950]]
 +[[https://​forum.openwrt.org/​viewtopic.php?​pid=99089#​p99089]]
 +
 +===== Backup OpenWrt configuration =====
 +
 +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>​
 +
 +Now on your local computer, use scp to retreive the file:
 +(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 ''​[[doc:​howto:​notuci.config#​etcsysupgradeconf|/​etc/​sysupgrade.conf]]''​ and [[doc:​howto:​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: .... FIXME
 +