4

My question is related to the part towards the end of this short article ("SD card image"): https://www.raspberrypi.org/documentation/linux/filesystem/backup.md

My question is how can I backup following this procedure but directly to a connected USB key ? Could someone enlighten me as to the command line to do this uncompressed (no Gzip) and with compression (with gzip) please ? Thanks !

When using df I get:

Filesystem 1K-blocks Used Available Use% Mounted on /dev/root 7698356 4037056 3247196 56% / devtmpfs 469532 0 469532 0% /dev tmpfs 473864 0 473864 0% /dev/shm tmpfs 473864 6460 467404 2% /run tmpfs 5120 4 5116 1% /run/lock tmpfs 473864 0 473864 0% /sys/fs/cgroup /dev/mmcblk0p6 67434 20862 46572 31% /boot tmpfs 94776 0 94776 0% /run/user/1000 /dev/mmcblk0p9 5792848 735388 4740148 14% /media/pi/root1 /dev/mmcblk0p8 61302 7014 54288 12% /media/pi/boot0 /dev/mmcblk0p5 30701 398 28010 2% /media/pi/SETTINGS /dev/sda1 30262832 18321360 11941472 61% /media/pi/D T

D T being my USB key. I have on the same SD card Raspbian and Recalbox, ideally I would like the img file to contain the whole SD Card as is, with Raspbian and Recalbox inside the ".img" (or ".iso") file.

MFJC
  • 235
  • 1
  • 3
  • 12

3 Answers3

2

You can do this by exactly the method given in the link you have pasted.

Warning: Please use these methods very carefully as they can irreparably damage your system data if done incorrectly.

Open a terminal using CTRL+ALT+T if you are directly connected.

Type in the command df to get a list of all your mounted partitions. You should see something like this:

/dev/mmcblk0p1    126976     56704     70272  45% /yousdcard
/dev/sdb        7433892   3280904   3752324  47% /yourflashdrive

So you know that the first entry corresponds to your sd card and the second one corresponds to your flash drive.

Please note that your SD card location can be different like sda1, sda2, sda3, sda4

You can then type:

sudo dd bs=4M if=/dev/mmcblk0p1 of=/yourflashdrive/raspbian.iso

Note that "if" stands for input file and "of" stands for output file in the above command.

This should backup your SD card to your home directory.

For tar and gzip, enter the following commands:

tar -zcvf /yourflashdrive/raspbian.tar.gz dev/mmcblk0p1
CoderX
  • 186
  • 5
  • Have you actually tried this? It is unclear what /dev/sda contains, but trying to backup an image to itself is bound to fail! – Milliways May 02 '17 at 00:07
  • @Milliways - Works perfectly. I haven't run into any issues. – CoderX May 02 '17 at 01:00
  • By definition an image is the same size as the SD Card, it cannot possibly fit in itself! You appear to be using Berryboot, which I believe uses compressed images so you probably aren't backing up the whole card, but it is unlikely to work for a normal installation. Not that the question needed an answer, as it contains a link describing how to backup an image. – Milliways May 02 '17 at 01:08
  • @Milliways - You're actually right. It will cause issues on normal installations. I have edited the answer to remove the confusion. – CoderX May 02 '17 at 01:40
  • @Milliways - Maybe so, but everyone needs clear explanation sometimes. Especially when they're starting out. – CoderX May 02 '17 at 01:41
  • @coderx Thanks a lot for your help ! I'll test it asap (probably tonight or tomorrow) and let you know ! – MFJC May 02 '17 at 14:21
  • Didn't understand this part though, is it to zip the img on the flash drive or zip it before copying it to the flash drive ? "For tar and gzip, enter the following commands: tar -zcvf /yourflashdrive/raspbian.tar.gz dev/mmcblk0p1" – MFJC May 02 '17 at 14:24
  • @coderx At the link I posted in my question they mention the ".img" and not the ".iso" extension you used for the image, is there a difference ? Should I still use ".img" ? Sorry for the question in the question, it's just to clarify a bit. Thanks again ! – MFJC May 02 '17 at 14:26
  • @Espressotron - Regarding the zip part. The syntax is tar -zcvf name-of-archive.tar.gz /path/to/directory-or-file If you do not understand how a tar archive is made, you'd have to raise a different question. Also, source here: link – CoderX May 02 '17 at 18:10
  • @Espressotron you can either use an ".img" or a ".iso". Most SD card imaging software like Rufus use ".img". It is the standard format. The reason I have used ".iso" here is because you had referenced your software of preference, Etcher. Upon reviewing their software, it seemed like they work better with ".iso". It is up to you. – CoderX May 02 '17 at 18:12
  • @coderx Thanks for your help. I mentioned ".img" and Etcher because I saw that on the Raspberry Pi documentation regarding the installation of new images. – MFJC May 04 '17 at 20:50
  • @Espressotron - Alright. So, were you able to set up the backup? Did this work for you? – CoderX May 04 '17 at 21:57
  • Didn't get the chance to use my Pi recently, I'll try in the coming days and let you know asap ! – MFJC May 04 '17 at 22:20
  • @Espressotron Okay. bstipe's methods below are good method's. However, note that they are separate. The first one 'sudo dd bs=4M if=/dev/sdb | gzip > rasbian.img # pi example' will export your pi device to raspbian.img which you can use with Etcher.The second one 'sudo dd bs=4M if=/dev/sdb | gzip | dd bs=4M of=/dev/sdc # USB Key' will mirror your pi drive to the USB key and can't be used with Etcher. The third one needs the second one so you can restore from the USB key direct. These are all perfectly acceptable ways to backup. – CoderX May 05 '17 at 17:59
  • @Espressotron - If you're really unsure of the command line, I suggest using berryboot to manage your installs on the pi because it has a gui version of cloning the drives and backing them up. – CoderX May 05 '17 at 18:00
  • @coderx but there is is no way to backup while piping to gzip and all that with a USB stick as destination all in one go without having a step on the SD card ? – MFJC May 05 '17 at 20:28
  • @coderx Hi there, I'm trying to backup (finally got to do it today), when I use df it lists a bunch of stuff but no /dev/mmcblk0p1 but only have /dev/mmcblk0p6 for /boot /dev/mmcblk0p9 for /media/pi/root1 /dev/mmcblk0p8 for /media/pi/boot0/dev/mmcblk0p5 for /media/pi/SETTINGS`. – MFJC May 08 '17 at 09:57
  • @coderx should I backup /dev/mmcblk0p6 for /boot ? or /dev/root/ (this one being "mounted on /"). – MFJC May 08 '17 at 09:58
  • @coderx an important detail to give, I have recalbox also setup on this SD Card as a dualboot with Raspberry. I'm a bit lazy to start over with berry boot (thanks for the tip though) as I just finished configuring all my Raspbian stuff. – MFJC May 08 '17 at 09:59
  • @coderx when I simply try sudo dd bs=4M if=/dev/mmcblk0p1 of=/yourflashdrive/raspbian.iso with of=/etc/sda1 for my flash drive and if=/dev/mmcblk0p1 for my sd card, it doesn't work and says dd: failed to open '/dev/sda1/backup.iso': Not a directory. I get the same error if I try with of=/media/pi/USBDrive/backup.iso. – MFJC May 08 '17 at 10:18
  • @Espressotron - The trouble with piping commands through | is that sudo owns only the first set. The second gzip command is run as your normal user. Also, if you really want to accomplish this in a single go, you can 'sudo su' to become root for a temporary while. Warning: It is generally not advised to become root as you can irreparably damage your system. I suggest you check your permissions on your usb key. Normally, if the filesystem is an NTFS one, you will not be able to get write permissions. Can you edit your original question and post your partition table from the 'df' command? – CoderX May 08 '17 at 10:39
  • @coderx Alright thanks for this info. Strange for the permissions since I formatted it to FAT to use with Mac and Pi. I originally formatted the USB Drive on Mac though. Regarding piping, the main goal of doing everything in one go was to not have a step where the backup is stored on the SD Card in the fear that it was gonna be too big. – MFJC May 08 '17 at 10:46
  • @coderx But when I use the basic command from Raspberry docs it using if=/dev/mmcblk0p6 as my source (is this the whole sd card anyway?), it does create an img file in my home that's barely 69 Mb, so either if=/dev/mmcblk0p6 isn't the whole sd card (since it's several Gb large) or Gzip has the compression power of a black hole !!! – MFJC May 08 '17 at 10:46
  • @Espressotron - The way the devices work are, your SD card will be '/dev/mmcblk0' They will then be partitioned into /dev/mmcblk0p1, dev/mmcblk0p2, dev/mmcblk0p3. The same with /dev/sda/ into /dev/sda1, /dev/sda2 – CoderX May 08 '17 at 10:47
  • @Espressotron- So you can just include the entire '/dev/mmcblk0 in your input file. – CoderX May 08 '17 at 10:48
  • @coderx Ah ok good to know thanks ! However I don't have a /dev/mmcblk0, I'm gonna post the df result right now as you requested. – MFJC May 08 '17 at 10:48
  • Including the whole '/dev/**mmcblk0** will backup my SD card with my 2 OSes (Raspbian and Recalbox) with their partitions etc into one ".img" file ? – MFJC May 08 '17 at 10:49
1

Assume the Raspbian.image is on /dev/sdb and Disk-to-Disk copy to /dev/sdc instead of to a filename (no gzip). I would suggest first to verify which device is the Raspbian.image and which is the USB Key, adjust /dev/sd? as required.

sudo dd bs=4M if=/dev/sdb of=raspbian.img  # raspberrypi.org example
sudo dd bs=4M if=/dev/sdb of=/dev/sdc      # connected USB Key

Restore:

sudo dd bs=4M if=/dev/sdc of=/dev/sdb

Gzip:

sudo dd bs=4M if=/dev/sdb | gzip > rasbian.img                  # pi example

# My bad, I forgot that zip, gzip don't work well with pipes
# sudo dd bs=4M if=/dev/sdb | gzip | sudo dd bs=4M of=/dev/sdc    # USB Key
# sudo dd bs=4M if=/dev/sdc | gunzip | sudo dd bs=4M of=/dev/sdb  # restore
bstipe
  • 534
  • 3
  • 5
  • thanks for your help ! However I'm confused between your techniques and the ones of @coderx. To me it seems @coderx first does the backup then compresses it with tar and gzip. You on the other and do the backup and zip it at the same time by piping it to gzip and then copy it to the USB key all in one command with: sudo dd bs=4M if=/dev/sdb | gzip | dd bs=4M of=/dev/sdc # USB Key correct ? – MFJC May 05 '17 at 12:11
  • You are correct. In Linux there are many different ways to do just about everything. I normally choose one that I feel good about and gets the job done. I am working on a script to backup my running system, so if the system card fails, just replace it with the backup and I am up and running. It will be similar to https://raspberrypi.stackexchange.com/questions/65838/raspbian-on-usb-stick/65871#65871, minus the 2nd and last step. – bstipe May 05 '17 at 12:40
  • @coderx Would this work to (1)backup as an img file the whole SD, (2) zip it and (3)save it directly on the USB drive, all in one command ? : sudo dd bs=4M if=/dev/sdb | gzip > rasbian.img | dd bs=4M of=/dev/sdc ? – MFJC May 08 '17 at 09:50
  • Tried my "all in one command" and it didn't work. Says "permission denied" for /dev/sda1 (strange because I am admin with sudo privileges, could it be because I require a password for sudo?). For /dev/sdb it says "No such file or director", guess that has to do with the fact that Raspberry Pi documentation gives "sdb" as a default and I should instead use mmcblk0p6 as explained above but I don't know if number 6 is the right one. – MFJC May 08 '17 at 10:24
  • When I do use sudo dd bs=4M if=/dev/mmcblk0p6 | gzip > rasbian.img | dd bs=4M of=/media/pi/USBKey with mmcblk0p6 however it seems to work and reports that 69mb were copied but still says "permission denied" for the USB drive and nothing appears on the USB key. – MFJC May 08 '17 at 10:24
  • My mistake, the second dd command also needs sudo. I will try to change my answer. – bstipe May 08 '17 at 20:07
  • Thanks ! In the end all this formatting and testing seems to have corrupted my test SD, heard they were fragile but man ! – MFJC May 08 '17 at 20:11
  • https://raspberrypi.stackexchange.com/q/66842/62747 – MFJC May 08 '17 at 20:12
  • I hope you are not trying to backup your running Linux system. I thought that you had a different Linux system backing up two USB devices. You can backup root on a running system with the rsync command (with care) but not with the dd command as there are filesystems that are generated at boot/load time. – bstipe May 08 '17 at 20:22
  • What do you mean you hope "I'm not trying to back up my running Linux system" ? I just started with the solution offered in the documentation of the RPi, the link I posted with my question. – MFJC May 09 '17 at 03:01
  • If I had used rsync instead of dd could I have backed up my whole SD Card to an img file that can then create another bootable SD Card using an image flashing app like Etcher ? – MFJC May 09 '17 at 04:06
1

To answer your last question about using rsync instead of dd to create an image file to be used by Etcher, the answer is yes and no. Let me start at the beginning. I have not used Etcher, I have used the Pi to create its own bootable clone/backup sd card from the running system. From this sd card or stick the dd, tar, zip, gzip, type of commands can be safely used by Etcher to create an image file. The setup takes some effort, but it is easy after the setup. I apologize for the mistakes in my previous answer, I will try to correct them.

SETUP - From your addition information I see that you are running Raspbian and Recalbox and I think NOOBS was installed. I have downloaded and installed NOOBS on a used reformatted sd card, unzipped it in place, shutdown/power off, inserted in system sd slot, inserted a new sd card in a USB adaptor, powered up, selected Raspbian and Recalbox, and configured with raspi-config. I copied from my old system card, /etc/hostname, /etc/hosts, /etc/ssh/, and /home/pi/.ssh/ and rebooted.

PARTITIONS - The sd (serial disk, sda) card/stick/key/HD has sectors which hold data. Partitions (primary, extended and logical; sda1, sda2, sda5) divides the sectors into groups to hold filesystems. The logical subdivides the extended. Some useful commands are df -h, lsblk, sudo fdisk -l /dev/sda, sudo blkid, ... for partition information.

pi@RPi5:~ $ df -h
Filesystem       Size  Used Avail Use% Mounted on 
/dev/root         15G  3.7G  9.8G  28% /
devtmpfs         459M     0  459M   0% /dev
tmpfs            463M     0  463M   0% /dev/shm
tmpfs            463M  6.4M  457M   2% /run
tmpfs            5.0M  4.0K  5.0M   1% /run/lock
tmpfs            463M     0  463M   0% /sys/fs/cgroup
/dev/mmcblk0p6    65M   21M   45M  32% /boot
tmpfs             93M     0   93M   0% /run/user/1000
/dev/sda1         30G   32K   30G   1% /media/pi/9016-4EF8
/dev/mmcblk0p9   1.9G  710M  1.1G  39% /media/pi/root0
/dev/mmcblk0p8    60M  6.8M   53M  12% /media/pi/boot0
/dev/mmcblk0p5    30M  5.4M   23M  20% /media/pi/SETTINGS
/dev/mmcblk0p10   11G   27M  9.8G   1% /media/pi/share

pi@RPi5:~ $ lsblk
NAME         MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda            8:0    1 29.8G  0 disk
└─sda1         8:1    1 29.8G  0 part /media/pi/9016-4EF8
mmcblk0      179:0    0 29.8G  0 disk
├─mmcblk0p1  179:1    0  2.8G  0 part 
├─mmcblk0p2  179:2    0    1K  0 part
├─mmcblk0p5  179:5    0   32M  0 part /media/pi/SETTINGS
├─mmcblk0p6  179:6    0   66M  0 part /boot
├─mmcblk0p7  179:7    0 14.4G  0 part /
├─mmcblk0p8  179:8    0   60M  0 part /media/pi/boot0
├─mmcblk0p9  179:9    0    2G  0 part /media/pi/root0
└─mmcblk0p10 179:10   0 10.6G  0 part /media/pi/share

CREATE PARTITIONS - Create partitions on /dev/sda to match thoses on /dev/mmcblk0 with values from the sudo fdisk -l /dev/mmcblk0 command. Your system will have different values that mine as I see that I made a mistake for download NOOBS to the sd card which is included in mmcblk0p1. I also see that I have mmcblk0p10 which is not in your mounts, don't include it if you don't have it (take the default end sector for mmcblk0p9). Some information lines are cut from the fdisk command to shorten the output.

pi@RPi5:~ $ sudo fdisk -l /dev/mmcblk0

Disk /dev/mmcblk0: 29.8 GiB, 32026656768 bytes, 62552064 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: 0x000daefb

Device          Boot    Start      End  Sectors  Size Id Type
/dev/mmcblk0p1           2048  5773437  5771390  2.8G  e W95 FAT16 (LBA)
/dev/mmcblk0p2        5773438 62552063 56778626 27.1G  5 Extended
/dev/mmcblk0p5        5775360  5840893    65534   32M 83 Linux
/dev/mmcblk0p6        5840896  5976063   135168   66M  c W95 FAT32 (LBA)
/dev/mmcblk0p7        5980160 36184061 30203902 14.4G 83 Linux
/dev/mmcblk0p8       36184064 36306941   122878   60M  c W95 FAT32 (LBA)
/dev/mmcblk0p9       36306944 40402941  4095998    2G 83 Linux
/dev/mmcblk0p10      40402944 62552063 22149120 10.6G 83 Linux

pi@RPi5:~ $ sudo umount /dev/sda?*
pi@RPi5:~ $ sudo fdisk /dev/sda

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

Command (m for help): o
Created a new DOS disklabel with disk identifier 0x3dbbc516.

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-62552063, default 2048): [Enter]
Last sector, +sectors or +size{K,M,G,T,P} ... default 62552063): +2.8G

Command (m for help):t
Selected partition 1
Hex code (type L to list all codes): e
Changed type of partition 'Linux' to 'W95 FAT16 (LBA)'.

Command (m for help): n
Partition type

Select (default p): e
Partition number (2-4, default 2): 2
First sector (5834752-62552063, default 5834752): [Enter]
Last sector, ... default 62552063): [Enter]

Command (m for help): n
Partition type
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): l

Adding logical partition 5
First sector (5836800-62552063, default 5836800): [Enter]
Last sector,  default 62552063): +32M

Command (m for help): n

Select (default p): l

Adding logical partition 6
First sector (5904384-62552063, default 5904384): [Enter]
Last sector, ... default 62552063): +66M

Command (m for help): t
Partition number (1,2,5,6, default 6): [Enter]
Hex code (type L to list all codes): c

Command (m for help): n

Select (default p): l

Adding logical partition 7
First sector (6041600-62552063, default 6041600): [Enter]
Last sector, ... default 62552063): +14.4G

Command (m for help): n

Select (default p): l

Adding logical partition 8
First sector (36222976-62552063, default 36222976): [Enter]
Last sector,  default 62552063): +60M

Command (m for help): t
Partition number (1,2,5-8, default 8): [Enter]
Hex code (type L to list all codes): c

Command (m for help): n

Select (default p): l

Adding logical partition 9
First sector (36347904-62552063, default 36347904): [Enter]
Last sector, ... default 62552063): +2G  # take default here if no 10

Command (m for help): n

Select (default p): l

Adding logical partition 10
First sector (40544256-62552063, default 40544256): [Enter]
Last sector, ... default 62552063): [Enter]

Command (m for help): p
Disk /dev/sda: 29.8 GiB, 32026656768 bytes, 62552064 sectors
...
Disklabel type: dos
Disk identifier: 0x3dbbc516

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sda1           2048  5834751  5832704  2.8G  e W95 FAT16 (LBA)
/dev/sda2        5834752 62552063 56717312   27G  5 Extended
/dev/sda5        5836800  5902335    65536   32M 83 Linux
/dev/sda6        5904384  6039551   135168   66M  c W95 FAT32 (LBA)
/dev/sda7        6041600 36220927 30179328 14.4G 83 Linux
/dev/sda8       36222976 36345855   122880   60M  c W95 FAT32 (LBA)
/dev/sda9       36347904 40542207  4194304    2G 83 Linux
/dev/sda10      40544256 62552063 22007808 10.5G 83 Linux

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

pi@RPi5:~ $ sudo fdisk -l /dev/sda

Disk /dev/sda: 29.8 GiB, 32026656768 bytes, 62552064 sectors
...
Disklabel type: dos
Disk identifier: 0x3dbbc516

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sda1           2048  5834751  5832704  2.8G  e W95 FAT16 (LBA)
/dev/sda2        5834752 62552063 56717312   27G  5 Extended
/dev/sda5        5836800  5902335    65536   32M 83 Linux
/dev/sda6        5904384  6039551   135168   66M  c W95 FAT32 (LBA)
/dev/sda7        6041600 36220927 30179328 14.4G 83 Linux
/dev/sda8       36222976 36345855   122880   60M  c W95 FAT32 (LBA)
/dev/sda9       36347904 40542207  4194304    2G 83 Linux
/dev/sda10      40544256 62552063 22007808 10.5G 83 Linux

CREATE FILESYSTEMS -

pi@RPi5:~ $ sudo mkfs.vfat -n RECOVERY /dev/sda1
pi@RPi5:~ $ sudo mkfs.ext4 -L SETTINGS /dev/sda5
pi@RPi5:~ $ sudo mkfs.vfat -n boot /dev/sda6
pi@RPi5:~ $ sudo mkfs.ext4 -L root /dev/sda7
pi@RPi5:~ $ sudo mkfs.vfat -n boot /dev/sda8
pi@RPi5:~ $ sudo mkfs.ext4 -L root /dev/sda9
pi@RPi5:~ $ sudo mkfs.ext4 -L share /dev/sda10

CLONE/COPY DATA INTO FILESYSTEMS - Raspbian will mount extra filesystems on /media/pi/... with owner pi and will create errors with the rsync command. By making new mount points and mounting the filesystems there, errors will not be created. The rsync -axvHAXW command will not cross filesystems, so /dev, /proc, /run, etc (system generated filesystems) and mounted filesystems (/mnt/d1, etc) will not be cloned/copied. It is best to not have other jobs running on the system during this section.

pi@RPi5:~ $ sudo umount /media/pi/*
pi@RPi5:~ $ sudo mkdir /mnt/d1 /mnt/d2
pi@RPi5:~ $ sudo mount /dev/mmcblk0p1 /mnt/d1
pi@RPi5:~ $ sudo mount /dev/sda1 /mnt/d2
pi@RPi5:~ $ sudo rsync -axvHAXW /mnt/d1/ /mnt/d2/  # always use trailing /
pi@RPi5:~ $ sudo umount /mnt/d?                    # on directors

pi@RPi5:~ $ sudo mount /dev/mmcblk0p5 /mnt/d1
pi@RPi5:~ $ sudo mount /dev/sda5 /mnt/d2
pi@RPi5:~ $ sudo rsync -axvHAXW /mnt/d1/ /mnt/d2/
pi@RPi5:~ $ sudo umount /mnt/d?

pi@RPi5:~ $ sudo mount /dev/sda6 /mnt/d2   # /boot is already mounted
pi@RPi5:~ $ sudo rsync -axvHAXW /boot/ /mnt/d2/
pi@RPi5:~ $ sudo umount /mnt/d2

pi@RPi5:~ $ sudo mount /dev/sda7 /mnt/d2   # / (root) is already mounted
pi@RPi5:~ $ sudo rsync -axvHAXW / /mnt/d2/
pi@RPi5:~ $ sudo umount /mnt/d2

pi@RPi5:~ $ sudo mount /dev/mmcblk0p8 /mnt/d1
pi@RPi5:~ $ sudo mount /dev/sda8 /mnt/d2
pi@RPi5:~ $ sudo rsync -axvHAXW /mnt/d1/ /mnt/d2/
pi@RPi5:~ $ sudo umount /mnt/d?

pi@RPi5:~ $ sudo mount /dev/mmcblk0p9 /mnt/d1
pi@RPi5:~ $ sudo mount /dev/sda9 /mnt/d2
pi@RPi5:~ $ sudo rsync -axvHAXW /mnt/d1/ /mnt/d2/
pi@RPi5:~ $ sudo umount /mnt/d?

pi@RPi5:~ $ sudo mount /dev/mmcblk0p10 /mnt/d1  # skip section if no 10
pi@RPi5:~ $ sudo mount /dev/sda10 /mnt/d2
pi@RPi5:~ $ sudo rsync -axvHAXW /mnt/d1/ /mnt/d2/
pi@RPi5:~ $ sudo umount /mnt/d?

REBOOT AND SYSTEM MOUNTS FILESYSTEMS -

pi@RPi5:~ $ sudo reboot

pi@RPi5:~ $ lsblk
NAME         MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda            8:0    1 29.8G  0 disk
├─sda1         8:1    1  2.8G  0 part
├─sda2         8:2    1    1K  0 part
├─sda5         8:5    1   32M  0 part /media/pi/SETTINGS
├─sda6         8:6    1   66M  0 part /media/pi/boot1
├─sda7         8:7    1 14.4G  0 part /media/pi/root1
├─sda8         8:8    1   60M  0 part /media/pi/boot
├─sda9         8:9    1    2G  0 part /media/pi/root
└─sda10        8:10   1 10.5G  0 part /media/pi/share
mmcblk0      179:0    0 29.8G  0 disk
├─mmcblk0p1  179:1    0  2.8G  0 part
├─mmcblk0p2  179:2    0    1K  0 part
├─mmcblk0p5  179:5    0   32M  0 part /media/pi/SETTINGS1
├─mmcblk0p6  179:6    0   66M  0 part /boot
├─mmcblk0p7  179:7    0 14.4G  0 part /
├─mmcblk0p8  179:8    0   60M  0 part /media/pi/boot0
├─mmcblk0p9  179:9    0    2G  0 part /media/pi/root0
└─mmcblk0p10 179:10   0 10.6G  0 part /media/pi/share1

# If root or boot are not configured as /dev/mmcblk0... in the
# cmdline.txt and/or /etc/fstab files, then adjustments will have
# to be configured to the proper device. Check with the following
# two commands:

pi@RPI5:~ $ for x in /boot /media/pi/boot*; do ls $x/cmdline.txt; cat $x/cmdline.txt; done | awk '{ if (NF == 1) fn = $1; else for (i=1; i<NF; i++)   if (substr($i,1,5) == "root=") print fn "  " $i }'

... root=/dev/mmcblk0p7 ...       # Good
... root=PARTUUID=000daefb-07 ... # Reconfigure sda to sda PARTUUID

pi@RPi5:~ $ for x in / /media/pi/root*; do ls $x/etc/fstab; cat $x/etc/fstab; done | grep -E 'fstab|/boot|/ ' | grep -v '#'

... /dev/mmcblk0p6 ...

DONE - The backup/clone card (sda) is basically the same as the system card (mmcblk0). If everything went right, the system can be shutdown sudo shutdown -h now, powered off (unplug power cord), and swap the backup card with the system card. Power up and the system should boot up just like before.

pi@RPi5:~ $ sudo blkid /dev/mmcblk0 /dev/sda
/dev/mmcblk0: PTUUID="3dbbc516" PTTYPE="dos"
/dev/sda: PTUUID="000daefb" PTTYPE="dos"

Notice the PTUUID="3dbbc516" on /dev/mmcblk0 is the same as Disk identifier: 0x3dbbc516 when I partitioned /dev/sda earlier. Anyway, BEFORE YOU REMOVE THE BACKUP SD CARD run sudo umount /dev/sda?* to sync the data, check unmounted status with df -h or lsblk command.

Also note if this sd card is placed into another system, the hostname, network config, and ssh keys/config may need to be changed.

To keep the backup current, refer to the CLONE/COPY DATA INTO FILESYSTEMS section as a guide. Don't remount root, boot, or others being used, they are already mounted.

You should be able to restore filesystem from the sda card/stick to the mmcblk0 sd card, but not the ones being used, (root, boot, or others).

The question about the bad sd card. Put it in a USB slot, run command lsblk, if it shows up as sda or sdb, you should be able to format it, if it does not show up, the sd card translate tables are shot and the card is trash.

bstipe
  • 534
  • 3
  • 5