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.

3 Answers 3

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
28
  • 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 2, 2017 at 0:07
  • @Milliways - Works perfectly. I haven't run into any issues.
    – CoderX
    May 2, 2017 at 1: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 2, 2017 at 1:08
  • @Milliways - You're actually right. It will cause issues on normal installations. I have edited the answer to remove the confusion.
    – CoderX
    May 2, 2017 at 1:40
  • @Milliways - Maybe so, but everyone needs clear explanation sometimes. Especially when they're starting out.
    – CoderX
    May 2, 2017 at 1:41
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
11
  • 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 5, 2017 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 raspberrypi.stackexchange.com/questions/65838/…, minus the 2nd and last step.
    – bstipe
    May 5, 2017 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 8, 2017 at 9: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 8, 2017 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 8, 2017 at 10:24
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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.