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
zh-cn:doc:howto:generic.backup [2018/03/03 20:55] – ↷ Links adapted because of a move operation bobafetthotmailzh:docs:guide-user:installation:generic.backup [2021/12/26 06:27] (current) – update sleppy
Line 1: Line 1:
-====== 通用备份 ====== +====== 通用 NOR 备份 ======
-===== 建立备份 ===== +
-再一次强调,请先查看 [[docs:techref:flash.layout#Details]],并注意[[docs:techref:bootloader]]分区和''ART''、''NVRAM''等等小分区没有包含在OpenWrt固件。因而某些错误导致这些分区数据被损坏,将不能恢复它!你的机器可能已经无法启动,你不得不去论坛问其它人要这些分区的备份,然后发送给你,你还要替换MAC地址然后通过 [[docs:hardware:port.jtag]] 恢复。+
  
-前所述,这些分区不是OpenWrt固件的一部分,刷OpenWrt固件不会改写他们。因而常不太可能受损,但并不是不可能,所以你应该做一个备份。+  * 本指南介绍了过[[https://openwrt.org/docs/guide-quick-start/sshadministration|CLI]]执行**块级**备份/恢复。 
 +  * **文件级**备份/恢复见[[https://openwrt.org/docs/guide-user/troubleshooting/backup_restore|备份和恢复]]
  
-如果ART分区损坏,仍然能够启动OpenWrt,只是无线网络不再正常工作。简单修复办法 ''[[docs:techref:mtd]]''。 +<WRAP center round important 60%> 
-<code> +基于 NAND 的设备应使用[[https://openwrt.org/docs/techref/flash#nand-specific_tools_for_reading_and_writing_to_raw_nand|NAND-aware程序]],因为''%%dd%%''不能正确处理 NAND 闪存的纠错或坏块标记。 
-dd if=/dev/$(grep '"art"' /proc/mtd |cut -c 1-4) of=/tmp/art.backup+</WRAP> 
 + 
 + 
 +请查看[[https://openwrt.org/docs/techref/file_system|file_system]]和[[https://openwrt.org/docs/techref/flash.layout#details|flash 布局详细信息]],并注意OpenWrt 仅涵盖''%%firmware%%''部分。**[[https://openwrt.org/docs/techref/bootloader|(引导程序)bootloader]]分区**,''%%ART%%''/''%%NVRAM%%''和类似的分区不是OpenWrt的固件的一部分。如果出现问题并且这些分区上的数据意外损坏,您将无法通过公共 OpenWrt 源替换它! 
 + 
 +由于 OpenWrt 不会写入这些分区,因此它们不太可能被 OpenWrt 本身损坏。但是,如果您想折腾bootloader部分,当然应该在您的 PC 上创建此数据的副本。否则,万一您丢失了该数据,您将不得不去论坛,让某人备份他的数据并发送给您,然后您必须更换MAC地址,然后通过[[https://openwrt.org/docs/techref/hardware/port.jtag|port.jtag将]]其[[https://openwrt.org/docs/techref/hardware/port.jtag|刷写]],否则您的设备可能无法再启动。 
 + 
 +===== 创建 ART 备份 ===== 
 + 
 +如果您的 ART 分区损坏仍然可以启动 OpenWrt,只是您的无线设备无法正常工作。使用''%%mtd%%''可以轻松修复 
 + 
 +<code bash
 +dd if=/dev/$(sed -n -e '/:.*"art"/s///p' /proc/mtd) of=/tmp/art.backup
 </code> </code>
-如果bootloader分区损坏,不会有 [[docs:techref:bootloader#additional.functions|booloader console]],只能通过 [[docs:hardware:port.serial|Serial Port]] 连接 [[docs:hardware:port.jtag|JTAG Port]] 来恢复或焊下flash芯片,参阅 [[docs:guide-user:troubleshooting:generic.debrick]] 获得帮助。 
  
-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: +如果您的引导加载程序分区已损坏,您甚至无法使用bootloader的[[https://openwrt.org/docs/techref/bootloader#additional_functions|控制台]]该控制台只能通过[[https://openwrt.org/docs/techref/hardware/port.serial|串行端口]]访问),的恢复方法是通过[[https://openwrt.org/docs/techref/hardware/port.jtag|JTAG 端口]]或通过拆焊闪存芯片,请参阅[[https://openwrt.org/docs/guide-user/troubleshooting/generic.debrick|generic.debrick]]寻求帮助。 
-不理解这些,通过如下代码备份 + 
-(大概意思,一但出现这情况想办法重写这些数据的备份将派上用场: +一旦您已经准备好再次写入闪存,您仍然需一些可以入的东西,会起作用的东西。在的备份将派上用场: 
-<code>+ 
 +<code bash>
 dd if=/dev/mtd0 of=/tmp/boot.backup dd if=/dev/mtd0 of=/tmp/boot.backup
 </code> </code>
-然后通过scp或ssh复制备份文件到您的电脑并妥善保存,可能有需要它们的时候。 
  
-===== 恢复备份 ===== +然后通过 scp 或 ssh 将您的备份文件复制到您的 PC 上,并确保它们的安全以供您可能需要它们。 
-===== 备份配置 ===== + 
-<code> +===== 创建完整的 MTD 备份 ===== 
-cd /etctar -cvzf config.tar.gz config+ 
 +该脚本假设在原生类 Unix 或 WSL 环境中使用Bash 和SSH。如果您更改了路由器的IP地址,请将 OPENWRT 变量值更改为OpenWrt 路由器的主机名/ IP。这会将您的 mtd 内容备份到''%%mtd_backup.tgz%%''与脚本相同的文件夹中的压缩 tarball 文件中。 
 + 
 +<code bash> 
 +cat << "EOF" > mtdbk.sh 
 +#!/bin/bash 
 +  
 +set -e 
 +  
 +function die() { 
 +    echo "${@}" 
 +    exit 2 
 +
 +  
 +OUTPUT_FILE="mtd_backup.tgz" 
 +OPENWRT="root@openwrt.lan" 
 +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 -o "ControlMaster=yes" -o "ControlPath=${SSH_CONTROL}" -o "ControlPersist=10" -n -N "${OPENWRT}" 
 +  
 +# This is the command we'll use to reuse the master connection 
 +SSH_CMD=(ssh -o "ControlMaster=no" -o "ControlPath=${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}\"" 
 +EOF 
 +chmod +x mtdbk.sh 
 +./mtdbk.sh 
 +</code> 
 + 
 +===== 从 OpenWrt 创建完整的 MTD 备份 ===== 
 + 
 +上述方法效果很好,但前提是您对路由器具有SSH根访问权限。在某些情况下,当您没有对路由器的SSH根访问权限,但可以从 UART 控制台连接时。例如带有原始官方固件的 TP-Link Archer C9 HW ver 5.0。您可以从路由器备份到您的主机。 
 + 
 +<code bash
 +# Save the script 
 +cat << "EOF"/tmp/backup.sh 
 +#!/bin/sh 
 +  
 +BACKUP_HOST="pc.lan" 
 +BACKUP_USER="root" 
 +echo "Backup host ${BACKUP_HOST}" 
 +  
 +cat /proc/mtd | tail -n+2 | while readdo 
 +  MTD_DEV=$(echo ${REPLY} | cut -f1 -d:) 
 +  MTD_NAME=$(echo ${REPLY} | cut -f2 -d\"
 +  echo "Backing up ${MTD_DEV} (${MTD_NAME})" 
 +  dd if=/dev/${MTD_DEV}ro | ssh -y ${BACKUP_USER}@${BACKUP_HOST} "dd of=~/${MTD_DEV}_${MTD_NAME}.backup" 
 +done 
 +EOF 
 +  
 +# Run the script 
 +sh /tmp/backup.sh 
 +</code> 
 + 
 +现在,您需要从 PC 为每个 mtd 设备输入5 次或更多次的SSH密码 。 
 + 
 +操作完成后,您将在PC 上的用户主目录中找到所有文件。 
 + 
 +如果您使用 linux 并希望查看进度,您可以在 PC 上的单独控制台中运行: 
 + 
 +<code bash> 
 +watch -n 0.2 ls -l --block-size=K ~ 
 +</code> 
 + 
 +===== 从引导加载程序创建备份 ===== 
 + 
 +有时可能需要从原始固件备份设置/分区。根据引导加载程序,可能有不同的策略。 
 + 
 +闪存芯片被映射到一个起始地址。使用 uboot 它应该在以下设置中: 
 + 
 +<code bash> 
 +printenv 
 +bdinfo 
 +</code> 
 + 
 +内存转储到串口(uboot: //md// ; redboot: //dump//)。将转储写入 tftp 或 nfs。 
 + 
 +===== 从引导加载程序控制台恢复备份 ===== 
 + 
 +许多引导加载程序允许您使用 mtd 分区,但要注意:它们不必与内核 mtd 分区相同!此外,对于某些引导加载程序,您不能使用 mtd-partition,您必须使用偏移量。在后一种情况下,在进行备份时记下这些正确的偏移量可能是个好主意。 
 + 
 +===== 从 OpenWrt 控制台恢复备份 ===== 
 + 
 +<code bash> 
 +mtd write art.backup art
 </code> </code>
-通过ssh等复制到其它地方。 
-==== Bootloader 控制台 ==== 
-大多数bootloader允许使用mtd分区, 但要注意: 他们不一定是相同的内核MTD分区! 此外, 有一些bootloader, 你不能使用mtd分区, 必须使用绝对位移. 在这种情况, 在你备份时写下这些正确的位移可能是一个好主意. 
  
-==== OpenWrt操作 ==== +述方法可能可以使用,可能无法正常使用,因为art分区通常不可写,因此您必须在做一些[[https://openwrt.org/toh/tp-link/tl-wr1043nd#making_bootloader_partition_writable|小修改]]后编译自己的内核。然后你必须把它刷到你的设备上,动它,现在分区应该是可写的
-试试: <code>mtd write art.backup art</code> +
-代码可以正常工作, 最有可能art分区通常不可写, 所以你需要自己编译个做了细微修改的内核,如这里描述的: +
-[[toh/tp-link/tl-wr1043nd#making.readonly.mtd.partition.writable|TL-WR1043ND]] +
-或这里: [[https://forum.openwrt.org/viewtopic.php?pid=135278#p135278]]. +
-然后写入你的设备, 重现在分区是可写的+
-From: +
-[[https://forum.openwrt.org/viewtopic.php?pid=134950#p134950]] +
-[[https://forum.openwrt.org/viewtopic.php?pid=99089#p99089]]+
  
  • Last modified: 2021/12/26 06:27
  • by sleppy