树莓派系统备份为img的最好方法
支付宝内搜索 9155838 即可领现金红包 每天都能领哦
首先,不要用Win32DiskImager备份,不然备份出来文件超大。
用下面这个脚本备份文件才比实际使用空间大一点点。
将如下脚本保存为/tmp/back2img.sh
#!/bin/bash
if [ `whoami` != "root" ];then
echo "This script must be run as root!"
exit 1
fi
# install software
#apt update
apt install -y dosfstools parted kpartx rsync
echo ""
echo "software is ready"
file="rpi-`date +%Y%m%d%H%M%S`.img"
if [ "x$1" != "x" ];then
file="$1"
fi
# boot mount point
boot_mnt=`findmnt -n /dev/mmcblk0p1 | awk '{print $1}'`
root_info=`df -PT / | tail -n 1`
root_type=`echo $root_info | awk '{print $2}'`
dr=`echo $root_info | awk '{print $4}'`
db=`df -P | grep /dev/mmcblk0p1 | awk '{print $2}'`
ds=`echo $dr $db |awk '{print int(($1+$2)*1.2)}'`
echo "create $file ..."
dd if=/dev/zero of=$file bs=1K count=0 seek=$ds
#truncate -s ${ds}k $file
start=`fdisk -l /dev/mmcblk0| awk 'NR==9 {print $2}'`
end=`fdisk -l /dev/mmcblk0| awk 'NR==9 {print $3}'`
if [ "$start" == "*" ];then
start=`fdisk -l /dev/mmcblk0| awk 'NR==9 {print $3}'`
end=`fdisk -l /dev/mmcblk0| awk 'NR==9 {print $4}'`
fi
start=`echo $start's'`
end=`echo $end's'`
end2=`fdisk -l /dev/mmcblk0| awk 'NR==10 {print $2}'`
end2=`echo $end2's'`
echo "start=$start"
echo "end=$end"
echo "end2=$end2"
parted $file --script -- mklabel msdos
parted $file --script -- mkpart primary fat32 $start $end
parted $file --script -- mkpart primary ext4 $end2 -1
loopdevice=`losetup -f --show $file`
device=`kpartx -va $loopdevice | sed -E 's/.*(loop[0-9])p.*/\1/g' | head -1`
device="/dev/mapper/${device}"
echo "device=$device"
partBoot="${device}p1"
partRoot="${device}p2"
echo "partBoot=$partBoot"
echo "partRoot=$partRoot"
sleep 5s
opartuuidb=`blkid -o export /dev/mmcblk0p1 | grep PARTUUID`
opartuuidr=`blkid -o export /dev/mmcblk0p2 | grep PARTUUID`
npartuuidb=`blkid -o export ${partBoot} | grep PARTUUID`
npartuuidr=`blkid -o export ${partRoot} | grep PARTUUID`
boot_label=`dosfslabel /dev/mmcblk0p1 | tail -n 1`
root_label=`e2label /dev/mmcblk0p2 | tail -n 1`
mkfs.vfat -F 32 -n "$boot_label" $partBoot
echo "$partBoot format success"
mkfs.ext4 $partRoot
e2label $partRoot $root_label
echo "$partRoot format success"
mount -t vfat $partBoot /mnt
cp -rfp ${boot_mnt}/* /mnt/
sed -i "s/$opartuuidr/$npartuuidr/g" /mnt/cmdline.txt
sync
umount /mnt
mount -t ext4 $partRoot /mnt
if [ -f /etc/dphys-swapfile ]; then
SWAPFILE=`cat /etc/dphys-swapfile | grep ^CONF_SWAPFILE | cut -f 2 -d=`
if [ "$SWAPFILE" = "" ]; then
SWAPFILE=/var/swap
fi
EXCLUDE_SWAPFILE="--exclude $SWAPFILE"
fi
cd /mnt
rsync --force -rltWDEgop --delete --stats --progress \
$EXCLUDE_SWAPFILE \
--exclude ".gvfs" \
--exclude "$boot_mnt" \
--exclude "/dev" \
--exclude "/media" \
--exclude "/mnt" \
--exclude "/proc" \
--exclude "/run" \
--exclude "/snap" \
--exclude "/sys" \
--exclude "/tmp" \
--exclude "lost\+found" \
--exclude "$file" \
/ ./
if [ ! -d $boot_mnt ]; then
mkdir $boot_mnt
fi
if [ -d /snap ]; then
mkdir /mnt/snap
fi
for i in boot dev media mnt proc run sys boot; do
if [ ! -d /mnt/$i ]; then
mkdir /mnt/$i
fi
done
if [ ! -d /mnt/tmp ]; then
mkdir /mnt/tmp
chmod a+w /mnt/tmp
fi
cd
sed -i "s/$opartuuidb/$npartuuidb/g" /mnt/etc/fstab
sed -i "s/$opartuuidr/$npartuuidr/g" /mnt/etc/fstab
sync
umount /mnt
kpartx -d $loopdevice
losetup -d $loopdevice
用法,直接运行:
cd /tmp/
sudo ./back2img.sh blog.dngz.net.img
注意:如果TF卡剩余空间不够的话就挂载外部硬盘或U盘,将备份的img直接保存在外部磁盘中,并且外部磁盘请挂载到 /media 目录下,不要挂载到 /mnt!
因为脚本会对/mnt目录进行备份操作,会导致不断循环的递归备份,导致磁盘爆满。
总之记住一定不要挂载到/mnt目录!
运行时的备份日志:
Reading package lists... Done
Building dependency tree
Reading state information... Done
dosfstools is already the newest version (4.1-2).
parted is already the newest version (3.2-25).
rsync is already the newest version (3.1.3-6).
The following NEW packages will be installed:
kpartx
0 upgraded, 1 newly installed, 0 to remove and 12 not upgraded.
Need to get 37.8 kB of archives.
After this operation, 86.0 kB of additional disk space will be used.
Get:1 https://mirrors.tuna.tsinghua.edu.cn/debian buster/main arm64 kpartx arm64 0.7.9-3+deb10u1 [37.8 kB]
Fetched 37.8 kB in 1s (47.2 kB/s)
Selecting previously unselected package kpartx.
(Reading database ... 49050 files and directories currently installed.)
Preparing to unpack .../kpartx_0.7.9-3+deb10u1_arm64.deb ...
Unpacking kpartx (0.7.9-3+deb10u1) ...
Setting up kpartx (0.7.9-3+deb10u1) ...
Processing triggers for man-db (2.8.5-2) ...
software is ready
create blog.dngz.net.img ...
0+0 records in
0+0 records out
0 bytes copied, 0.00126612 s, 0.0 kB/s
start=8192s
end=532479s
end2=532480s
device=/dev/mapper/loop0
partBoot=/dev/mapper/loop0p1
partRoot=/dev/mapper/loop0p2
mkfs.fat 4.1 (2017-01-24)
mkfs.fat: warning - lowercase labels might not work properly with DOS or Windows
/dev/mapper/loop0p1 format success
mke2fs 1.44.5 (15-Dec-2018)
Discarding device blocks: done
Creating filesystem with 739328 4k blocks and 185104 inodes
Filesystem UUID: ed9bf852-cd2f-4c1b-a9f6-d1e00bd20844
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912
Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done
/dev/mapper/loop0p2 format success
sending incremental file list
rsync同步数据日志省略...
Number of files: 62,378 (reg: 48,951, dir: 5,618, link: 7,809)
Number of created files: 62,376 (reg: 48,951, dir: 5,616, link: 7,809)
Number of deleted files: 0
Number of regular files transferred: 48,951
Total file size: 2,260,121,595 bytes
Total transferred file size: 2,259,998,604 bytes
Literal data: 2,259,998,604 bytes
Matched data: 0 bytes
File list size: 1,703,776
File list generation time: 0.008 seconds
File list transfer time: 0.000 seconds
Total bytes sent: 2,264,157,862
Total bytes received: 990,956
sent 2,264,157,862 bytes received 990,956 bytes 6,214,400.05 bytes/sec
total size is 2,260,121,595 speedup is 1.00
备份后blog.dngz.net.img 显示3.1G(实际占用2.4G),用7z极限压缩后仅为630M。
本文撰于2019年12月11日,置于草稿箱,今日才发布。
更新于:2022-05-05 20:23:21 栏目:技术技巧 关键词:树莓派,树莓派备份
本站使用「署名 4.0 国际」创作共享协议,可转载、引用,但需署名作者且注明文章出处
还没折腾过树莓派,不知道性能怎么样……
好歹搜一下shrink这个词,你也不至于复制粘贴出来这么多代码垃圾。
@石樱灯笼
搜了,不懂。
看的头晕,这么多代码