1

I've got an issue with making the backup from microSD 16GB card. My Raspbian installation takes up 2GB and the other space is empty.

When I tried to use dd on my MacBook, I've got an image with 15.93GB.

How can I make a backup without empty space? e.g. image, which size is equal with the size of my Raspbian (it's about 2GB).

Aurora0001
  • 6,198
  • 3
  • 21
  • 37
romankh3
  • 125
  • 1
  • 1
  • 13
  • 1
    `dd` (device dump?) doesn't care about filesystems, so it will copy the whole block device (physical disk or partition) without regards to files folders etc - can MacBook's read ext4 filesystems? – Jaromanda X Mar 20 '19 at 10:18
  • Ok, I asked about the way of how to backup without empty data. Is it a bad question to mark it as `-1`? – romankh3 Mar 20 '19 at 11:30
  • @Jaromanda X, macOS wont recognize an ext4 filesystem by default but you can add support (e.g. via MacFuse). The output file from dd can just be a file ... in which case it doesn't matter if the mac can attach and use it as a file (it would work as a backup in that you could use dd to restore from it.) – Tim Campbell Mar 20 '19 at 16:55

2 Answers2

2

There are some possibilities to reduce the size of the backup from a SD Card. You can use rsync as suggested by @binki, or you can use the classic method to do a backup with tar. With the last one you must mount the ext4 filesystem but macOS doesn't know ext4.

So I suggest another method. You can zero-out the free space on the SD Card. This will not reduce the physical size of the image but it can be compressed very much to nearly the size of the used space for data. There is a pseudo device file /dev/zero that will output zeros. On a running Raspberry Pi you can copy it to a zerofile as big as the unused space on the SD Card. Of course it will take some time doing it. For an 1.7 GB zerofile I get this:

rpi ~$ time dd if=/dev/zero of=zerofile bs=4M
dd: error writing 'zerofile': No space left on device
434+0 records in
433+0 records out
1817587712 bytes (1.8 GB, 1.7 GiB) copied, 368.33 s, 4.9 MB/s

real    6m43.538s
user    0m0.002s
sys     0m15.563s

As you see the command will terminate with an error when the disk is full. Just delete zerofile and poweroff the RasPi:

rpi ~$ rm zerofile
rpi ~$ sudo systemctl poweroff

Now you can take a compressed image from the SD Card. Put it into the card reader attached to the laptop. Then assuming it is seen as /dev/sdb:

# On OS X use lower case 'bs=4m'
laptop ~$ sudo bash -c 'dd if=/dev/sdb bs=4M | gzip > backup.img.gz'

To restore do:

# On OS X use lower case 'bs=4m'
laptop ~$ sudo bash -c 'gzip -cd backup.img.gz | dd of=/dev/sdb bs=4M conv=fsync'
Ingo
  • 40,606
  • 15
  • 76
  • 189
  • unfortunately, I made first step, without bs=4M, because I've got an error "dd: bs: illegal numeric value". After that, the last command wasn't right for me... Have you tried to do it? – romankh3 Mar 22 '19 at 17:41
  • I've done the last step in another way - I used `balenaEtcher` app to flash backup.img.gz. – romankh3 Mar 22 '19 at 18:09
  • @RomanBeskrovny I have done it many times before but haven't tested just this commands I have given here. I will do it now. Maybe I have overseen something. I'm doing it on a laptop with **Debian**. Maybe you have different versions of `gzip` and `dd` with other options? – Ingo Mar 22 '19 at 18:11
  • @RomanBeskrovny OK, using `balenaEtcher`. Seems it decompresses out of the box. I'm interested why the last command fails on a MacBook. What's the error message? – Ingo Mar 22 '19 at 18:22
  • it sad that the resource is busy... – romankh3 Mar 22 '19 at 18:57
  • @RomanBeskrovny Thanks. I have corrected the last two commands. Forgot to execute them as root (sudo) and to decompress `gzip -cd` the image. – Ingo Mar 22 '19 at 20:54
  • I will try. Nevertheless, It's very helpful for me. I made a backup with 900MB size. It's awesome! – romankh3 Mar 23 '19 at 06:31
  • @RomanBeskrovny Yes, `gzip` counts the number of continuous zeros and stores only that number. – Ingo Mar 23 '19 at 08:29
1

In short - you need to use rsync.

Have a look at this answer Can a Raspberry Pi be used to create a backup of itself?.
If you plan on keeping the backups on another machine also check my answer on a related question Restore backup failed (using rsync)

binki
  • 61
  • 2
  • Am I right understand, that this is for the case, when you want to make the array os the backups? – romankh3 Mar 23 '19 at 12:15
  • I am not sure I understand your question, but if you ask about the 'keeping the backups on another machine' part, this is because you can have user ownership mismatches if you store the backups without archiving them on another machine, because the other machine might not have the same users as the raspberry pi. – binki Mar 25 '19 at 20:57