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.
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.
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
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
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. Usersync
. https://raspberrypi.stackexchange.com/a/5492/5538 – goldilocks Jan 11 '20 at 19:44rsync
? maybe I should experiment withrsync [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:53rsync -av [src] [dst]
many times to back up complete file systems. – Stabledog Jan 11 '20 at 20:11dd if=/dev/zero of=/tmp/zero ...
, then the result file will be small. – Philippe Jan 11 '20 at 20:22rsync
– Fran Anquela Jan 12 '20 at 10:56dd
other withrsync
as well. – Fran Anquela Jan 12 '20 at 11:33dd
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