Teach your Raspberry Pi system backup method tutorial Guinness. 4B (full card + compressed backup)

Quote: https://www.hao4k.cn/thread-41764-1-1.html


VMware is installed on Windows with Linux virtual machine features a 'snapshot', is to make a backup of your current system, once hung out later misuse the system can be restored to the time that the backup node. Linux open system permissions relatively high, with root privileges if the operation is likely to be accidentally deleted some of the system files will cause the system to crash. I had the Raspberry Pi system hung out several times, each time only to re-brush into the mirror, have a lot of repeat operations after power on, so raspberry pie backup system is very important. Also you could have been deployed Raspberry Pi system, bulk copy to come in more raspberries.

Backup system is divided into
"full-calorie Backup" and
"compressed backup" alone. "Full card backup" is the SD card overall backup, simple operation, SD card is mirroring how much space there is much need for greater SD card System Restore job. "Compression Backup" only backup the content of the SD card partition, basic backup out of the image and the original image file size is similar to the method a bit complicated, mirroring backup volume is greatly reduced. Official Raspberry Pi after the original image will be automatically extended throughout the SD card space after the first start, restore a backup image first turn need raspi-config expansion SD card space sudo.
First, the whole card backup
1. Win32DiskImager
full backup card is the simplest one, first create a file on the hard disk empty img suffix, open Win32DiskImager, select an empty img file and SD card drive letter you just created, click read can .

Wait a few minutes after the img file has been backed up with a time of Win32DiskImager to create a mirror because they can not read Linux partitions, so is the whole card backup, the disadvantage of this method is consistent with the capacity to back up files and SD card, but also in reducing when you must use a larger capacity than mirroring SD card.

Raspbian can see the original system only 3.5G, the backup file is 7.4G (8G SD card).
Raspberry come in direct replication system 2.
Prepare a blank SD card, formatted SD card into the reader, then insert come raspberry, umount uninstall the mount, use the command dd bs = 4M if = / dev / mmcblk0 of = / dev / sda the raspberry Pi system can copy all the contents to the SD card, mmcblk0 is to come in raspberry system TF card, sda is to insert an SD card (using lsblk view), you can either wait for the complete shutdown insert a new card restart.

In addition to the original system is copied to the SD card, but can also be copied to removable storage devices like U disk, raspberry pie 3B + support directly from the USB boot system, raspberry pie 4B is not yet supported.
Blkid get a good system has been burning U disk or removable hard disk PARTUUID through sudo.

The system TF card /boot/cmdline.txt in rootpartuuid modify the above uuid restart, you can boot the system from a USB device.


The method essentially the original system image is written to another SD card for copying your system to partner with small, not suitable for use as a backup.


Raspberry Pi desktop system comes with SD Card copier, without entering commands can also be completed replication process.
3. dd command
raspberry put to mount a shared directory (TF card space than the system), and then directly back up dd raspberry can send dd if = / dev / mmcblk0 of = raspberrypi.img bs = 1M shared directory suggested words on the computer network cable connection, wireless network will be very slow.
Or operation (automatically mount SD card, does not automatically mount the SD card you'll need to manually mount) in Ubuntu virtual machine. In the figure below / dev / sdb1 / dev / sdb2 Raspberry Pi is an SD card, which is partition sdb1 256M boot partition, partition sdb2 7.1G Raspberry Pi is a file system partition.

Use the dd command to back up SD card directly. Raspberry Pi path of SD card here is / dev / sdb1 and / dev / sdb2, so the entire SD card backup path is / dev / sdb.
Enter the backup command: sudo dd if = / dev / sdb | gzip> /home/hanson/Documents/rpi-back2.gz, which path you want to save the backup file location, file name, and the SD card to the actual selection. When the backup terminal no progress bar, like stuck, like, waiting on the line. Can open another terminal, run the following command can be observed change in the target file size.
watch -d -n 5 ls -lh /home/hanson/Documents/rpi-back2.gz

This command can be packaged at the same time the backup files, compressed .gz file after the completion of only 1.9G, after decompression is 7.5G, essentially but also the entire card copy.
Reduction when extracting files in Windows need to add .img suffix. Reduction method sudo gzip -dc /home/hanson/Documents/rpi-back2.gz under Linux | sudo dd of = / dev / sda
Second, the compressed backup
1. Principle
Raspberry Pi's official system is based on Debian, mainly two partitions: boot partition (boot), the root partition (root), burning system when the two partitions is written to the SD card. Need to use raspi-config After the first boot to expand the size of the root partition, boot partition unchanged, to achieve the purpose of using all SD card contents amount (the latest system has no need to extend the operation).
For backup Raspberry Pi system, mainly backup boot and root partition. The root partition which is only part of the space to store the content, the rest is empty, if the direct backup, then it is the whole SD card backup space, that is, the above mentioned full card backup. The compressed backup is a backup boot partition and a root partition in the content space.


2. PiShrink cutting
PiShrink is open on Github Raspberry Pi compression tool with the above command by cutting or Win32DiskImager dd full image backup card, remove the content without a partition, so as to reduce the size of the backup image.

First full card copy backup image files to Linux, open a terminal execute wget  https://raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.sh  download sh script file, the default save it in / home / user directory which was copied to the folder where the document image. Execute chmod + x pishrink.sh increased execute permission, then execute sudo bash pishrink.sh rpi-back.img can be.


The original 7.5GB backup file is reduced to 3.4GB.
If your linux system language is Chinese, may be error, run sudo pishrink.sh raspberrypi.img English need to develop
methods sh script file tailoring is relatively simple, than if you simply want to use the command line to cut a full backup card in Linux file, you can use linux reference crop raspberry Pi sd card complete mirror
3. gparted
According to the principle of the compressed backup, first in the root partition split a space and blank space in the Linux environment, then read the contents of space on the Windows backup software with Win32DiskImager only. The following to Windows 10 64 bits + virtual machine Ubuntu 19.10 as an example, raspberry pie Raspbian system 4B of compressed backups.
(1) Open virtual machine, enter in a terminal sudo apt install gparted install the GParted
(2). The need to back up the SD card into the reader and then inserted into the computer, wait for Linux to read successfully.
(3) Enter the sudo gparted start gparted, select the SD card in the upper right corner, you can see them set ext4 partition using 3.78G, the remaining 10.88G are blank.

(4). Right-click on the ext4 partition umount click Uninstall, and then continue right-click to change the size.


(5) In the pop-up window re-allocation size in a new column to fill than the minimum size (3689) three or four hundred large on the line, I have here a large 300M, will automatically calculate the free space in the click, and then click the lower right corner Resize, then you can see the original partition only 4.07G, 10.58G remaining unallocated space.

(6) and then click the green checkmark button to confirm in the dialog pop-up application.


(7) Wait for execution is complete, you can see the partition of 14.66G reduced to 4.07G, then eject the SD card, to operate on Linux done here.


(8) The SD card plugged into the back Windows, create a new blank img file, open Win32DiskImager software, select the file, then select the 'read only partition has been assigned' '(Note to the latest version, the old version does not have this Features).


(9) Wait for the completion of the reading, compression of image backup is finished, the source image file 3.9GB, image file backup of 4.5GB, if compress, then only 1.8GB, and for storage.


4. Raspberry Pi / Linux command line
this way to fully utilize the Linux command operation, use dump and restore to create a mirror img Raspberry Pi, the specific process from the CSDN Blog, I follow the text step practice a lot.

(1) View system capacity share of
first with df -h command to check the current share of capacity in order to determine the size of the backup image to be generated in the future, if the Raspberry Pi takes less than 50% SD card space, you can raspberry Pi is generated directly inside the mirror. Otherwise, your backup image generated to mount the external SD card inside.

Raspberry sent there can be seen two root and boot partition, occupied size of 3.5G + 253M = 3.8G, because the format conversion, it will be loss of 5% to 10%, the total size of about 3.8 * 1.1 = 4.2G. If the SD card so much greater than the remaining space can be followed down the paper.
Required (2) Software Installation
the sudo APT-GET the install the dosfstools the dump the parted The kpartx
the dosfstools: FAT32 partition formatting tools dump: dump & restore backup tool parted & kpartx: virtual disk tool
(3) The first step in the system calculated. space, create a blank image raspberrypi.img about 4.5G with the dd command, this process takes a few minutes to complete. sudo dd if = / dev / zero of = raspberrypi.img bs = 1M count = 4500
Note that bs = 1M (1M = 1024x1024Bytes) .

(4) dividing the virtual disk
learned by sudo fdisk -l / dev / mmcblk0: a first partition boot partition using FAT32 format, starting from sector 8192 to sector 532480; EXT4 using the second partition, starting from sector 540672 to the end of the blank img.

--script the parted raspberrypi.img the sudo - mklabel MSDOS
the sudo the parted raspberrypi.img --script - Primary FAT32 8192s 532480s the mkpart
--script the sudo the parted raspberrypi.img - the mkpart Primary 540672s -1 ext4
start sector number of the partition are multiples of 8192, the above command is basically fixed in 8192, 532480 and 540672 modified according to your own situation the actual query, -l represent the end of the file.
Check that the success of the partition sudo parted raspberrypi.img, parted in the program, enter print free command displays the contents of the partition, enter quit to exit.

. (5) and mount the virtual disk format
set img file corresponding Loop device: sudo losetup -f --show raspberrypi.img next command line output / dev / loop0. (If the various steps in the future if not loop0 in the loop0 must be changed here and the same).

Then sudo kpartx -va / dev / loop0


At this loop device is set up, loop0p1 corresponds / boot partition on img file, loop0p2 corresponding to the / (root).
Img file format and then two partitions.
-n the Boot mkfs.vfat sudo / dev / Mapper / loop0p1
sudo mkfs.ext4 / dev / Mapper / loop0p2

mount the loop device to target img file under the new directory.
mkdir tgt_boot tgt_Root sudo mount -t vfat -o uid = pi, gid = pi, umask = 0000 / dev / mapper / loop0p1 ./tgt_boot/ sudo mount -t ext4 / dev / mapper / loop0p2 ./tgt_Root/
here the tree berry pie inside the system do a backup operation, if it is with other Linux systems, please note uid and gid settings.
(6) Start Backup
[Backup / boot]
First, backup / boot, you can directly copy sudo cp -rfp / boot / * ./tgt_boot/ ( if using other Linux systems, / boot / * into your mount SD card, such as dev / sdb1)

runs out of out of a pile Tip: failed to preserve ownership for ..., enter the graphical interface to view the next:


files are copied successful, but the file permissions are not copied, little effect.

[Backup "/"]
Root file system with dump / restore method, first set the appropriate rights to the target mount point, and empty. (If other Linux systems, will replace the corresponding user name and user group pi.pi)
the sudo the chmod 777 tgt_Root the sudo chown pi.pi tgt_Root the sudo RM -rf ./tgt_Root/* tgt_Root CD /
before starting the backup
sudo dump -0uaf - / | sudo restore -rf - (If you are using other Linux systems, / into your mount SD card, such as dev / sdb2)

the entire dump / restore process takes a few minutes to see dUMP iS DONE it shows the backup was successful a.
If the above method sudo dump ... that Broken pipe, Illegal instruction errors such step occurs fails, you can use tar method, the package source SD card root file system, so the need for additional and source SD card in the machine system space the root file system used space as the size of the available space, the detailed process to see the original author.
. (7) modify the PARTUUID
case the backup has been completed, the need to modify the corresponding partition boot Raspbian PARTUUID, i.e. img modify the target file in the following two documents: ./ tgt_boot / cmdline.txt and ./tgt_Root/ etc / fstab
first look img file corresponding loop device is PARTUUID two partitions: sudo blkid

here you can see / boot corresponds 594e62d6-01, / corresponds 594e62d6-02
Modify cmdline.txt file sudo vi ./tgt_boot/cmdline.txt

modify the fstab file vim ./tgt_Root/etc/fstab sudo


(8) finishing work.
Uninstall each mounted partition: sudo umount tgt_boot tgt_Root
delete the loop device: sudo kpartx - d / dev / loop0 and sudo losetup -d / dev / loop0
delete mount point directory: rmdir tgt_boot tgt_Root
last only img file

to copy it to the Windows computer save by WinSCP. Raspberry or sent directly in dd if = / home / xxx / xxx.img of = / dev / sdb bs = 4M; sync programmed img file to another SD card (used if = image directory specified, external U disk of = the address of the device used to specify not necessarily the SDB, the last write-once designated bs = 4M bytes, sync synchronization data), after running in the same raspberry Pi, the first with a first raspi-config partition space expand it.
This method, although more steps, but shining go step by step down may have detailed knowledge of all the technical details of the entire backup.
5. sh script a backup key
step of compressing pure Linux command line cumbersome backup, all of the steps can be written in a script file .sh, so only needs to execute the script file will automatically executing the compressed backup commands.
Using the above script only need to create a .sh file sudo vi backup.sh, sudo chmod 777 backup.sh change permissions in raspberry pie, and then sudo ./backup.sh can generate a backup raspberry pie in the current script directory image file, and then also you need to manually modify the next PARTUUID. After using either the Windows Win32DiskImager the recovery image to the SD card, you can restore to the SD card using the dd command in Linux, when the start of Raspberry Pi with this SD card performs raspi-config -> Expand Filesystem extension can not space used.
sh script first needs to be able to read in order to use, which some parameters need to be modified to suit your own environment. sh script if an error is not easy to find the cause of the problem, it was suggested first Linux command all the processes to go through, come and use this script, so you can know which step wrong to modify.
6. Recommended script
there's a script is my most recommended, program content is too long, I do not Tieshanglai, the specific contents of the script I did not fully understand, easy to use anyway, is what I currently use a backup method , you can go here to download, use the following method:
step1: Download the script file rpi-backup.sh to a Linux system
step2: the need to back up the SD card into a Linux system, use the command df -h SD card corresponding to the device name under inquiry .

step3: rpi-backup.sh into the script file directory, only the following two commands to complete the SD card backup, eventually img file is generated in the ~ / backupimg / folder.
sudo chmod + x rpi-backup.sh (requiring to write executable permissions)
./rpi-backup.sh / dev / sdb1 / dev / sdb2 ( script execution has two parameters, the first parameter is the Raspberry Pi SD Card / boot partition device name: / dev / sdb1, the second parameter is / partition's device name: / dev / sdb2, subject to modification)

Third, sum up
the whole card backup is certainly not advisable if you are a novice white, it is recommended that you use on Windows Win32DiskImager or raspberries come in the SD card copier card full backup and then a cut, with the minimum amount of code PiShrink, simple and not prone to error, is time-consuming.
If you are familiar with the basic operation of the words that Linux can be backed up in a Linux virtual machine using gparted graphical interface.

Guess you like

Origin www.cnblogs.com/bongem/p/12312878.html