7

I backup my raspberry pi "live" and remote from another linux computer, over ssh with gzip compression. I use this script:

#!/bin/bash
dt=`date +%y%m%d`
ssh pi@192.168.1.141 "sudo dd if=/dev/mmcblk0 bs=1M | gzip -" | dd of=./pibackup-$dt.gz

It works really well, but the resultant file is 26Gb, and that's too much (I think). I would like to backup only the content of the system and not the free clusters. I know dd make a whole backup, including free clusters too, but I thought that with the use of gzip I could reduce the size of the resultant file.

So looks out my system, when I type df -h:

Filesystem      Size  Used Avail Use% Mounted on
/dev/root        29G  3.1G   24G  12% /
devtmpfs        459M     0  459M   0% /dev
tmpfs           464M     0  464M   0% /dev/shm
tmpfs           464M   49M  415M  11% /run
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           464M     0  464M   0% /sys/fs/cgroup
/dev/mmcblk0p1  253M   52M  201M  21% /boot
tmpfs            93M     0   93M   0% /run/user/1000
/dev/sda1       932G  184G  749G  20% /media/HDD
Paulo Tomé
  • 3,782
  • 5
    Although it's amongst the simplest, dd is probably the worst backup method ever for a number of reasons (and worst is not an exageration). It is very handy for many things, but it is not really a filesystem level tool. Use rsync. https://raspberrypi.stackexchange.com/a/5492/5538 – goldilocks Jan 11 '20 at 19:44
  • I use dd because it's the simplest way I know. can I make a full backup (of the whole system and partitions) using rsync ? maybe I should experiment with rsync [options] --rsh="ssh [ssh options]" root@[the pi ip]:/ /backup/rpi/ ... I'll try it and come back later. Thank you very much @goldilocks – Fran Anquela Jan 11 '20 at 19:53
  • I've used variations of rsync -av [src] [dst] many times to back up complete file systems. – Stabledog Jan 11 '20 at 20:11
  • 1
    What I usually do is zero-out free clusters first with dd if=/dev/zero of=/tmp/zero ..., then the result file will be small. – Philippe Jan 11 '20 at 20:22
  • @Philippe How does it works? Would it overuse the microSD card? – Fran Anquela Jan 11 '20 at 20:24
  • Have you tested the resultant backup file? I'd lay fairly strong odds that many of them result in corrupted filesystems when you restore. Backing up a raw live filesystem is a really really bad idea. – Chris Davies Jan 11 '20 at 22:10
  • Did you mount the file-system read-only (not writable), before copying it? – ctrl-alt-delor Jan 11 '20 at 22:49
  • 1
    @roaima I haven't tested it already. I must test it on a new SD, and then I will comment. For now, I use the solution from @goldilocks with rsync – Fran Anquela Jan 12 '20 at 10:56
  • @ctrl-alt-delor I think that would be really a good idea. Could you explain it as an answer please? This way, I think it would be possible to make a live backup over ssh with dd other with rsync as well. – Fran Anquela Jan 12 '20 at 11:33
  • I was only highlighting another reason that dd is not the best way to do it. rsync would be better. Another reason is that when you buy the new sd card to recover the backup, you will only use half of it, and want to then expand the file-system. (look at man mount, to get the answer from your last comment) – ctrl-alt-delor Jan 12 '20 at 11:48
  • @goldilocks I could not disagree more with this statement; dd is the only reliable method, and must be done while the OS is not running. You should always checksum the result, and don't try "to pad out" errors with zeros! rsync can be trivially confused between ownership/ids, does not know the difference between filesystems in memory and disk, and gives a false impression of safety when used on a live system. I use dd for failsafe image backups, and use rsync for user data only, because I know when it is in a "safe" state. Also, you should always test your backups by restoring! – m4r35n357 Nov 14 '23 at 11:36
  • @m4r35n357 please stop answering with half truths. The OP stated they were using dd to make backups of a live filesystem. The entire set of comments bar yours is saying don't do that. Yours starts by saying it's fine but then points out it can't be performed safely except on an unmounted filesystem. As for whether dd is a useful tool, you'll see if you look around that most of the time (including here) there are better rooms available even for bitwise copies – Chris Davies Nov 14 '23 at 13:51
  • @ChrisDavies Answering, or just commenting? Back to the point: I am saying "live backups" are inherently a bad idea, except for isolated "blobs" of unchanging data. This is the full truth. – m4r35n357 Nov 14 '23 at 14:04
  • @ChrisDavies I couldn't parse "better rooms" so I didn't (couldn't) comment on it . . . – m4r35n357 Nov 14 '23 at 14:10

3 Answers3

6

"Cold" backup is much safer than "live" backup.

On a running PI, run following command

cd $HOME; dd if=/dev/zero of=zero bs=8M; rm zero

It will write zeroes on all free space, which will give high compression rate. then put raspberry pi's microSD onto another system, for example, on a desktop running ubuntu and run

fdisk -l

I see microSD as /dev/sdb, then following command will compress the whole SD with :

gzip -c /dev/sdb > /backup/raspberry-pi.gz

To restore, run this command:

gzip -cd /backup/raspberry-pi.gz > /dev/sdb

It's recommended to take a full backup first before trying above procedure.

Philippe
  • 1,435
  • could you maybe add an explanation of why you are doing it like this and how this works to your answer? i.e. you're overwritting all free space with 0s which can be compressed much better by gzip. I think that could be even more useful for people coming here. – acran Jan 12 '20 at 07:41
  • It's not really what I was looking for, because it's "cold backup". I would use it for long-term backups, and the comment of @goldilocks rsync -av --delete --exclude-from=./full-backup-exclude-list.txt --rsh=ssh root@192.168.1.141:/ ./backup/full-backup for daily backups. I must now test it on a new SD card, to proof that it really works like I've expected – Fran Anquela Jan 12 '20 at 10:54
  • @Philippe you're right, but I'm looking for a "live backup" too – Fran Anquela Jan 12 '20 at 11:34
  • When I am doing a full backup, I do zero all of the remaining space too. +1 – Vlastimil Burián Jan 12 '20 at 15:06
  • I would also md5sum the output file against the SD card, and only compress if they match. – m4r35n357 Nov 14 '23 at 12:32
  • To zero out unused space, I would use (e.g from my home dir) the simpler: "cat /dev/zero >aaa; rm -f aaa". It is simpler, and also my /tmp is on tmpfs anyway ;) – m4r35n357 Nov 14 '23 at 13:12
  • 1
    @m4r35n357 You are right, answer updated. – Philippe Nov 14 '23 at 13:36
3

As suggested in the comments, you have a number of filesystem-level options for backing up the Pi. If you do this you will need to exclude the virtual filesystems /proc and /sys. You also should be aware that it will not include the non-filesystem boot image or your partition table; I'll provide a suggestion for backing that up separately at the end of this answer.

  1. You can use rsync. This will give you a copy of the filesystem on your backup media rather than that a single image file.

    rsync -avzHP --exclude '/proc/' --exclude '/sys/' root@pi:/ /backup/pi.$(date +'%Y%m%d')
    

    If your local account isn't root you will probably want to include the --fake-super option for backup and restore. (It saves the remote ownership details although it can't actually apply them to the local backup.)

    If you do decide to go this route I'd strongly suggest you also look at rsnapshot to give you GFS backups without much extra disk space being used.

  2. You can use tar, pax, or some other archiving tool. This will give you a compressed image

    ssh root@pi 'cd / && tar --exclude '/proc/' --exclude '/sys/' -czvf - .' > /path/to/backup.pi.$(date +'%Y%m%d').tgz
    
  3. You can use other options such as duplicity. I've not used this seriously so I don't think I'm best qualified to give an example of how it would be used.


Having got the filesystem backup you also, presumably, want to be able to restore the minimal boot image.

On my Pi we have this structure (I've ignored partition three; you may have one, but it's probably been backed up as part of your filesystem):

fdisk -l /dev/mmcblk0
Disk /dev/mmcblk0: 29.7 GiB, 31914983424 bytes, 62333952 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: 0x42913321

Device         Boot   Start      End  Sectors  Size Id Type
/dev/mmcblk0p1         8192   137215   129024   63M  c W95 FAT32 (LBA)
/dev/mmcblk0p2       137216  8388607  8251392    4G 83 Linux

You need everything from the start of the disk to the start of partition 1 (/boot). In this particular display the sectors are in 512 byte blocks but we need to read in 4KiB blocks (SSD block size), so we divide all the numbers by eight:

# Copy the boot segment from the beginning of the disk
dd bs=4k count=$((8192/8)) if=/dev/mmcblk0 | gzip >img0.gz

You would restore this saved segment to the SSD card at /dev/mmbclk0 with a command like this. Note that it will irretrievably overwrite the destination device, so check it several times before using it, and do not blindly copy this example:

zcat img0.gz | dd bs=4k iflag=fullblock of=/dev/mmcblk0
partprobe /dev/mmcblk0
Chris Davies
  • 116,213
  • 16
  • 160
  • 287
  • I'll test this advice on my pi. I think that's what I was looking for. But I would add a exclude-list.txt with these directories: /proc/* /sys/* /dev/* /boot/* /tmp/* /run/* /mnt/* /media/*, following the advice of @goldilocks Thanks to both – Fran Anquela Jan 13 '20 at 08:12
  • One question more: How can you restore the boot sectors? I want to make a test of backup and full restore in a new sdcard. I need the first boot sectors in img0.gz, the boot partition at mmcblk0p1 and the rootfs partition from mmcblk0p2, that I choosed to backup with rsync following the advice of @goldilocks and @roaima.

    I would like to make a script to backup and another one to restore (and post it here).

    – Fran Anquela Jan 13 '20 at 09:07
  • @FranAnquela try that updated answer – Chris Davies Jan 13 '20 at 14:39
  • Never use a backup tool unless you know exactly how it claims to work! – m4r35n357 Nov 14 '23 at 12:34
0

Here is my (trivial) dd-based image backup script for a Raspberry Pi SD card (to be run on another machine!):

#!/bin/sh

in=$1 out=$2

echo "Copying . . ." time -p dd if=$in of=$out bs=4M status=progress

echo "Checking Source . . ." set $(md5sum $in) sum_in=$1 echo $sum_in

echo "Checking Destination . . ." set $(md5sum $out) sum_out=$1 echo $sum_out

if [ $sum_in = $sum_out ] then echo "Compressing . . . " xz -6 --threads=2 --verbose $out && echo "DONE" else echo "Checksum mismatch!" fi

Save it as e.g. sd-verify-compress.

Make it executable:

$ chmod +x sd-verify-compress

Run it with (for example):

$ ./sd-verify-compress /dev/sdx bookworm64

Find what to use instead of /dev/sdx using lsblk:

$ lsblk -fmp

This is the file it creates:

-rw-r--r-- 1 pi pi 2.8G Nov 13 12:38 bookworm64.xz

My SD card is 32GB.

m4r35n357
  • 101