---
# System prepended metadata

title: System Backup

---

# System Backup
### 從image裡面擷取資料
#### - 秀出sdcard.img分割區資訊(記得使用root權限)

    $ sudo fdisk -lu sdcard.img


會跑出下列結果

    
    Disk sdcard.img: 152 MiB, 159384064 bytes, 311297 sectors
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disklabel type: dos
    Disk identifier: 0x00000000
    
    Device         Boot     Start     End   Sectors  Size Id Type
    sdcard.img1     *           1   65536     65536   32M  c W95 FAT32 (LBA)
    sdcard.img2             65537  311296    245760  120M 83 Linux


這裡可以看到總共有兩個分區，且Sector Size為512，可以得知第一區起始位置為1\*512，第二區為65537\*512

#### - 掛載兩個分區
    $ mkdir -p ~/mmn1
    $ mkdir -p ~/mmn2
    $ sudo mount -o loop,offset=$((1\*512)) sdcard.img ~/mmn1
    $ sudo mount -o loop,offset=$((65537\*512)) sdcard.img ~/mmn2

如果跳出以下錯誤：


    
    mount：bad usage    
    Try 'mount --help' for more information.
請檢查''-''是否為半形，且逗號後面不能有空格

        
    mount: /home/student/mmn2: overlapping loop device exists for /home/student/buildroot-2022.02/output/images/sdcard.img.
    
上述錯誤則表示掛載有部份重疊，可以選擇在掛第一分區的時候加上空間限制
    
    $ sudo mount -o loop,offset=$((1*512)),sizelimit=$((65536*512)) sdcard.img ~/mmn1
    $ sudo mount -o loop,offset=$((65537*512)) sdcard.img ~/mmn2


\
\

### 備份系統
#### -備份整個SDcard
先確定device file，這裡使用的位置在/dev/sde
    
    $ sudo dd if=/dev/sde of=backup.img
    
這樣就會產生一個backup.img的檔案。

#### -個別備份SDcard裡面的partition

    $ sudo dd if=/dev/sde1 of=boot_part.img
    $ sudo dd if=/dev/sde2 of=filesys_part.img

#### -使用rsync備份系統
先印出SDcard內部分區狀態
    
    $ sudo fdisk -ul /dev/sde
    
會顯示

    Welcome to fdisk (util-linux 2.34).
    Changes will remain in memory only, until you decide to write them.
    Be careful before using the write command.


    Command (m for help): p
    Disk /dev/sde: 14.86 GiB, 15931539456 bytes, 31116288 sectors
    Disk model: STORAGE DEVICE  
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disklabel type: dos
    Disk identifier: 0x00000000

    Device     Boot Start    End Sectors  Size Id Type
    /dev/sde1  *        1  65536   65536   32M  c W95 FAT32 (LBA)
    /dev/sde2       65537 311296  245760  120M 83 Linux

    Command (m for help): q


先掛載第二區，之後rsync的時候會用到
    
    $ sudo mount /dev/sde2 ~/mmn2
    
先產一個大小與我們要備份的資料一樣的image file，
bs=block size=1024(固定)，
count=數量=Sector*512/1024=245760/2=122880
    
    $ dd if=/dev/zero of=filesystem.img bs=1024 count=122880
印出資料如下
    
    122880+0 records in
    122880+0 records out
    125829120 bytes (126 MB, 120 MiB) copied, 0.291326 s, 432 MB/s
    
在image裡面先做分區
    
    $ sudo fdisk filesystem.img

    Welcome to fdisk (util-linux 2.34).
    Changes will remain in memory only, until you decide to write them.
    Be careful before using the write command.

    Device does not contain a recognized partition table.
    Created a new DOS disklabel with disk identifier 0x07b41cde.

    Command (m for help): n
    Partition type
       p   primary (0 primary, 0 extended, 4 free)
       e   extended (container for logical partitions)
    Select (default p): p
    Partition number (1-4, default 1): 1
    First sector (2048-245759, default 2048): 
    Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-245759, default 245759): 

    Created a new partition 1 of type 'Linux' and of size 119 MiB.

    Command (m for help): w
    The partition table has been altered.
    Syncing disks.

印出看一下結果

     $ sudo fdisk filesystem.img

    Welcome to fdisk (util-linux 2.34).
    Changes will remain in memory only, until you decide to write them.
    Be careful before using the write command.


    Command (m for help): p
    Disk filesystem.img: 120 MiB, 125829120 bytes, 245760 sectors
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disklabel type: dos
    Disk identifier: 0x07b41cde

    Device          Boot Start    End Sectors  Size Id Type
    filesystem.img1       2048 245759  243712  119M 83 Linux

    Command (m for help): q



接著要透過losetup去連結image file(建立一個pesudo device)，首先要先確定有哪些可以用的loop(如下可使用的就是loop13)
    
    $df -h
    
    Filesystem     1K-blocks     Used Available Use% Mounted on
    udev             8053132        0   8053132   0% /dev
    tmpfs            1617324     1960   1615364   1% /run
    /dev/sda5      859554832 50692908 765129188   7% /
    tmpfs            8086604   359908   7726696   5% /dev/shm
    tmpfs               5120        4      5116   1% /run/lock
    tmpfs            8086604        0   8086604   0% /sys/fs/cgroup
    /dev/loop0         56832    56832         0 100% /snap/core18/2128
    /dev/loop4         56960    56960         0 100% /snap/core18/2344
    /dev/loop7         55552    55552         0 100% /snap/snap-store/558
    /dev/loop5         63488    63488         0 100% /snap/core20/1405
    /dev/loop6         66816    66816         0 100% /snap/gtk-common-themes/1519
    /dev/loop8         44800    44800         0 100% /snap/snapd/15177
    /dev/loop3         66688    66688         0 100% /snap/gtk-common-themes/1515
    /dev/loop9         52224    52224         0 100% /snap/snap-store/547
    /dev/loop1        224256   224256         0 100% /snap/gnome-3-34-1804/72
    /dev/loop10       224256   224256         0 100% /snap/gnome-3-34-1804/77
    /dev/loop2        254848   254848         0 100% /snap/gnome-3-38-2004/99
    /dev/loop11          128      128         0 100% /snap/bare/5
    /dev/sda1          32686    28640      4046  88% /boot/efi
    tmpfs            1617320       52   1617268   1% /run/user/1000
    /dev/loop12        32686     9994     22692  31% /home/student/mmn1
    /dev/sde2         109525    60513     40411  60% /home/student/mmn2
    /dev/sde1          32686     9994     22692  31% /media/student/6C28-739B

然後就可以開始連結，sizelimit = (End-Start+1) = (245759-2048+1) = 243712
    
    $ sudo losetup --offset $((2048*512)) --sizelimit=$((243712*512)) /dev/loop13 filesystem.img
    
之後做格式化成ext4
    
    $ sudo mkfs.ext4 /dev/loop15
    
    
    mke2fs 1.45.5 (07-Jan-2020)
    Discarding device blocks: done                            
    Creating filesystem with 30464 4k blocks and 30464 inodes

    Allocating group tables: done                            
    Writing inode tables: done                            
    Creating journal (1024 blocks): done
    Writing superblocks and filesystem accounting information: done

然後就可以把設置好的image file掛載到某個資料夾路徑上
    
    $ sudo mount /dev/loop15 ~/mmn1
    
最後就可以開始做備份了，這裡再重新提及一下，
～/mmn1這個路徑掛載的是剛剛做的image file
～/mmn2這個路徑上掛載的是SD卡上的partition2 (filesystem部份)

    $ sudo rsync -axvH --delete ~/mmn2 ~/mmn1
    
之後會開始跑一大段東西，輸出的最後兩行會是以下結果，這樣就代表備份成功

    sent 60,934,828 bytes  received 36,544 bytes  40,647,581.33 bytes/sec
    total size is 60,806,410  speedup is 1.00

結束之後不要忘記要移除pesudo device以及掛載的東西
    
    $ sudo losetup -d /dev/loop15
    $ sudo umount /dev/sde1
    $ sudo umount /dev/sde2
    
    
### 測試
將BOOT或filesystem裡面的檔案刪除，在將備份的檔案dd進去看是否能正常開機。

    $ sudo dcfldd bs=1024 if=backup.img of=/dev/sdc  //可以秀出備份進度
    或是
    $ sudo dd if=backup.img of=/dev/sdc



    