1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
| #!/bin/sh
usbmount=/mnt/u
img=$usbmount/rpi-back-`date +%Y%m%d-%H%M`.img echo "img:$img"
echo ===================== part 3, create a new blank img ===============================
bootsz=`df -P grep /boot awk '{print $2}'` rootsz=`df -P grep /dev/root awk '{print $3}'` totalsz=`echo $bootsz $rootsz awk '{print int(($1+$2)*1.3)}'` echo "start create img,total size=${totalsz}K"
pv -tpreb /dev/zero sudo dd of=$img bs=1K count=$totalsz
bootstart=`sudo fdisk -l /dev/mmcblk0 grep mmcblk0p1 awk '{print $2}'` bootend=`sudo fdisk -l /dev/mmcblk0 grep mmcblk0p1 awk '{print $3}'` rootstart=`sudo fdisk -l /dev/mmcblk0 grep mmcblk0p2 awk '{print $2}'` echo "format virtual disk: boot: $bootstart >>> $bootend, root: $rootstart >>> end"
sudo parted $img --script -- mklabel msdos sudo parted $img --script -- mkpart primary fat32 ${bootstart}s ${bootend}s sudo parted $img --script -- mkpart primary ext4 ${rootstart}s -1 loopdevice=`sudo losetup -f --show $img` device=/dev/mapper/`sudo kpartx -va $loopdevice sed -E 's/.*(loop[0-9])p.*/\1/g' head -1` sleep 5 sudo mkfs.vfat ${device}p1 -n boot sudo mkfs.ext4 ${device}p2 echo "format finish"
echo ===================== part 4, fill the data to img =========================
mountb=$usbmount/backup_boot/ mountr=$usbmount/backup_root/ mkdir -p $mountb $mountr
sudo mount -t vfat ${device}p1 $mountb sudo cp -rfp /boot/* $mountb sync echo "...Boot partition done"
sudo mount -t ext4 ${device}p2 $mountr 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 sudo rsync --force -rltWDEgop --delete --stats --progress \ $EXCLUDE_SWAPFILE \ --exclude '.gvfs' \ --exclude '/dev' \ --exclude '/media' \ --exclude '/mnt' \ --exclude '/proc' \ --exclude '/run' \ --exclude '/sys' \ --exclude '/tmp' \ --exclude 'lost\+found' \ --exclude '$usbmount' \ // $mountr
for i in dev media mnt proc run sys boot; do if [ ! -d $mountr/$i ]; then sudo mkdir $mountr/$i fi done if [ ! -d $mountr/tmp ]; then sudo mkdir $mountr/tmp sudo chmod a+w $mountr/tmp fi
sync ls -lia $mountr/home/pi/ echo "...Root partition done"
opartuuidb=`blkid -o export /dev/mmcblk0p1 grep PARTUUID` opartuuidr=`blkid -o export /dev/mmcblk0p2 grep PARTUUID` npartuuidb=`blkid -o export ${device}p1 grep PARTUUID` npartuuidr=`blkid -o export ${device}p2 grep PARTUUID` sudo sed -i "s/$opartuuidr/$npartuuidr/g" $mountb/cmdline.txt sudo sed -i "s/$opartuuidb/$npartuuidb/g" $mountr/etc/fstab sudo sed -i "s/$opartuuidr/$npartuuidr/g" $mountr/etc/fstab
sudo umount $mountb sudo umount $mountr
|