我有一个简单的备份脚本,它还会将本地数据创建到外部 USB 设备的 tar/gzip 存档,然后将该存档复制到第二个 USB 设备。
例如:
usb1="/mnt/usbone"
usb2="/mnt/usbtwo"
source="/home/user"
tar cfz ${usb1}/source.tar.gz ${source}
cp -ar ${usb1}/source.tar.gz ${usb2}
这似乎可以优化为tar
在两个驱动器上创建副本,而不是创建一个存档,然后再复制。生成的存档相当小 (<1GB)。我知道这不是安全的备份方法。
编辑:我已经快速测试了 Archemar 的解决方案并比较了这些方法。为了更好地衡量,我还使用 测试了最初的方法rsync
。查看结果time
(不是 bash 时间,/usr/bin/time)以及我用来测试它的脚本。
源是用创建的dd if=/dev/urandom bs=1M count=1024 of=/tmp/random.blob
。主机是通过 microSD 卡运行的 Raspberry Pi 3B,安装的目标是 USB 2.0 闪存驱动器(${a}
和${b}
)。
a.sh(tar 和 cp):
tar cfz ${a}/r1.tar.gz ${s}
cp -ar ${a}/r1.tar.gz ${b}/r1.tar.gz
b.sh(tar 和 tee):
tar cfz - ${s} | tee ${a}/r2.tar.gz > ${b}/r2.tar.gz
c.sh(tar 和 rsync):
tar cfz ${a}/r3.tar.gz ${s}
rsync -aW ${a}/r3.tar.gz ${b}/r3.tar.gz
结果:
# /usr/bin/time -v bash a.sh
Command being timed: "bash a.sh"
User time (seconds): 218.71
System time (seconds): 28.33
Percent of CPU this job got: 68%
Elapsed (wall clock) time (h:mm:ss or m:ss): 6:03.13
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 2480
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 41
Minor (reclaiming a frame) page faults: 1250
Voluntary context switches: 45519
Involuntary context switches: 25576
Swaps: 0
File system inputs: 3668157
File system outputs: 4197336
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0
# /usr/bin/time -v bash b.sh
Command being timed: "bash b.sh"
User time (seconds): 221.64
System time (seconds): 28.62
Percent of CPU this job got: 85%
Elapsed (wall clock) time (h:mm:ss or m:ss): 4:53.98
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 2536
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 31
Minor (reclaiming a frame) page faults: 1162
Voluntary context switches: 68310
Involuntary context switches: 35582
Swaps: 0
File system inputs: 2101321
File system outputs: 4197832
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0
# /usr/bin/time -v bash c.sh
Command being timed: "bash c.sh"
User time (seconds): 235.24
System time (seconds): 35.01
Percent of CPU this job got: 74%
Elapsed (wall clock) time (h:mm:ss or m:ss): 6:04.03
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 2652
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 40
Minor (reclaiming a frame) page faults: 2310
Voluntary context switches: 65402
Involuntary context switches: 45179
Swaps: 0
File system inputs: 4200957
File system outputs: 4197496
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0
令我惊讶的是,结果并没有那么明显。File system inputs: 2101321
不过,使用 tar/tee 方法要低很多,我希望这对 SD 卡的寿命有好处。
答案1
你可以使用tee
命令
usb1="/mnt/usbone"
usb2="/mnt/usbtwo"
source="/home/user"
tar -cz -f - "${source}" | tee "${usb2}/source.tar.gz" > "${usb1}/source.tar.gz"
在哪里
-f -
告诉tar
使用 stdout 作为备份文件。tee "${usb2}/source.tar.gz"
将把标准输入复制到指定的文件和标准输出。> "${usb1}/source.tar.gz"
将重定向标准输出tee
到文件。