A different approach to backing up is described here that uses rsync and a copy-on-write filesystem to make incremental backups of files which have changed over time. As you can never have too many backups, one approach is to use the method described here for frequent backups while continuing to make occasional copies of the entire SD card. Having said this, I will admit that I have never made an image of an SD card for backup nor lost any data due to lack of backups. Note, however, in addition to being occasionally lucky, I also keep backups using dump and tar on separate drives and, of course, upload the really important stuff (like these notes) to other peoples computers.
In addition to a Raspberry Pi, the equipment needed consists of a suitable USB hard disk and a USB SD card reader. I will be using a Raspberry Pi 3B+, a Seagate 8TB desktop USB hard disk and a thumb sized SD card reader called Everything But Stromboli. These instructions have been tested with Raspbian Stretch up-to-date as of August 1, 2018. In what follows, a # indicates the prompt for a command that should be executed as root while $ indicates a command that can be executed as an unprivileged user. Do not type the # or $ symbols.
First, plug the hard disk into the Pi. In my case the drive was detected as /dev/sdb. Make sure it did not automount; otherwise unmount it. Next install gdisk and partition the drive using
Code: Select all
# apt-get install gdisk
# gdisk /dev/sdb
Command (? for help): o
This option deletes all partitions and creates a new protective MBR.
Proceed? (Y/N): y
Command (? for help): n
Partition number (1-128, default 1): 1
First sector (34-15628053133, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-15628053133, default = 15628053133) or {+-}size{KMGTP}:
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
Command (? for help): c
Using 1
Enter name: BTRFS Backup
Command (? for help): p
Disk /dev/sdb: 15628053167 sectors, 7.3 TiB
Logical sector size: 512 bytes
Disk identifier (GUID): C2F3503A-285F-4B87-87E4-3991095CA22B
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 15628053133
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)
Number Start (sector) End (sector) Size Code Name
1 2048 15628053133 7.3 TiB 8300 BTRFS Backup
Command (? for help): w
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.
Code: Select all
# mkfs.btrfs -L Backup /dev/sdb1
btrfs-progs v4.7.3
See http://btrfs.wiki.kernel.org for more information.
Label: Backup
UUID:
Node size: 16384
Sector size: 4096
Filesystem size: 7.28TiB
Block group profiles:
Data: single 8.00MiB
Metadata: DUP 1.00GiB
System: DUP 8.00MiB
SSD detected: no
Incompat features: extref, skinny-metadata
Number of devices: 1
Devices:
ID SIZE PATH
1 7.28TiB /dev/sdb1
# mkdir /mnt/Backup
# mount LABEL=Backup /mnt/Backup
Code: Select all
# cd /mnt/Backup
# mkdir raspberrypi
# cd raspberrypi
# btrfs sub creat boot
Create subvolume './boot'
# btrfs sub creat root
Create subvolume './root'
Code: Select all
#!/bin/sh
if cd /mnt/Backup/raspberrypi
then
bdir=boot-`date +'%Y.%m.%d'`
echo Creating snapshot $bdir...
rsync -glopqrtxDH --numeric-ids --delete /boot/ boot &&
btrfs sub snap -r boot $bdir
bdir=root-`date +'%Y.%m.%d'`
echo Creating snapshot $bdir...
rsync -glopqrtxDH --numeric-ids --delete / root &&
btrfs sub snap -r root $bdir
fi
Code: Select all
# chmod +x dobackup
Code: Select all
# ./dobackup
Creating snapshot boot-2018.08.02...
Create a readonly snapshot of 'boot' in './boot-2018.08.02'
Creating snapshot root-2018.08.02...
Create a readonly snapshot of 'root' in './root-2018.08.02'