Skip to content

Instantly share code, notes, and snippets.

@marbu
Created May 8, 2017 17:16
Show Gist options
  • Save marbu/ed5c2d2dc33e647b12307bb38306a9c8 to your computer and use it in GitHub Desktop.
Save marbu/ed5c2d2dc33e647b12307bb38306a9c8 to your computer and use it in GitHub Desktop.
Rhea Backup

Rhea Backup

Target device: external 1TB HDD via usb 3. Filesystem: btrfs

Strategy:

  • Whole /home for full user data backup.
  • Use lvm2 snapshots to freeze state of /home during backup. Also consider btrfs in the future.
  • Running under root, automated via script.
  • Use mirror like approach for easy access, while keeping multiple snapshots of older backup batches.
  • Easy read-only access to the old snapshots.

Backup software

Either one of:

  • file backup/mirroring to btrfs on external drive
  • file backup/mirroring to ext4 on external drive
  • rsync pushes to btrfs subvolume

Software to consider:

Or just btrfs snapshots with some script over it.

Btrfs

See:

To get disk usage:

# btrfs filesystem df /
# btrfs filesystem show /dev/sda
# btrfs filesystem usage / 

Subvolumes and snapshots:

# btrfs subvolume create /path/to/subvolume
# btrfs subvolume list path
# btrfs subvolume snapshot source [dest/]name

Compression:

# btrfs filesystem defragment -r -v -clzo /
# mount -o compress=lzo /dev/sdxY /mnt/

Send and Receive:

# btrfs send /root_backup | btrfs receive /backup

Details

Multiple fs trees, fs tree corresponds to a subvolume. The main tree has id 5, while first-created subvolume 256.

Withins fs tree, objects has independent inode number.

Mounting a subvolume also makes any of its nested child subvolumes available at their respective location relative to the mount-point.

A Btrfs filesystem has a default subvolume, which is initially set to be the top-level subvolume and which is mounted if no subvol or subvolid option is specified.

Tip: Changing subvolume layouts is made simpler by not using the toplevel subvolume (ID=5) as / (which is done by default). Instead, consider creating a subvolume to house your actual data and mounting it as /. See Flat Layout.

Changing the default subvolume with btrfs subvolume default will make the top level of the filesystem inaccessible, except by use of the subvol=/ or subvolid=5 mount options. Note: this is not what Fedora does or is suggested for Flat Layout scheme.

A snapshot is simply a subvolume that shares its data (and metadata) with some other subvolume, using Btrfs's COW capabilities.

Incremental Backup backed by btrfs use cases

Based on How can I use btrfs for backups/time-machine? and https://btrfs.wiki.kernel.org/index.php/Incremental_Backup on btrfs wiki:

In general:

See also complex tools (or not exactly fitting my use case):

Good example (on Arch Linux) when one uses btrfs on the workstation as well: https://ramsdenj.com/2016/04/05/using-btrfs-for-easy-backup-and-rollback.html (tools used: btrfs, snapper, btrbk, ..)

Actual setup

Create filesystem directly on the device:

# mkfs.btrfs -L wd_backup_disk /dev/sdb
btrfs-progs v4.6.1
See http://btrfs.wiki.kernel.org for more information.

Label:              wd_backup_disk
UUID:               ee43a6cf-01a8-419a-8e2e-3b995e418995
Node size:          16384
Sector size:        4096
Filesystem size:    931.48GiB
Block group profiles:
  Data:             single            8.00MiB
  Metadata:         DUP               1.01GiB
  System:           DUP              12.00MiB
SSD detected:       no
Incompat features:  extref, skinny-metadata
Number of devices:  1
Devices:
   ID        SIZE  PATH
    1   931.48GiB  /dev/sdb

# mount /dev/sdb /mnt/wd_backup_disk/

Testing rsyncbtrfs:

# btrfs subvolume create /mnt/wd_backup_disk/phoebe_snapshots
# mkdir /mnt/wd_backup_disk/phoebe_snapshots/martin
# rsyncbtrfs init /mnt/wd_backup_disk/phoebe_snapshots/martin/
# rsyncbtrfs backup /home/martin /mnt/wd_backup_disk/phoebe_snapshots/martin/
Create subvolume '/mnt/wd_backup_disk/phoebe_snapshots/martin//.inprog-CRIELdI/vol'
# ls -l /mnt/wd_backup_disk/phoebe_snapshots/martin/
total 4
drwx------. 1 martin martin 592 May  5 19:49 2017-05-08-13:22:39
lrwxrwxrwx. 1 root  root   19 May  8 13:34 cur -> 2017-05-08-13:22:39

Backing up home on rhea:

# lvcreate --size 10G --name lv_snap_home --snapshot /dev/vg_rhea/lv_home
# mkdir /mnt/snap_home
# mount -o ro /dev/vg_rhea/lv_snap_home /mnt/snap_home/
# mount /dev/sdg /mnt/wd_backup_disk/
# btrfs subvolume create /mnt/wd_backup_disk/rhea_home_snapshots
# rsyncbtrfs init /mnt/wd_backup_disk/rhea_home_snapshots
# rsyncbtrfs backup /mnt/snap_home/ /mnt/wd_backup_disk/rhea_home_snapshots
# umount /mnt/snap_home/
# lvremove /dev/vg_rhea/lv_snap_home
# ls -l /mnt/wd_backup_disk/rhea_home_snapshots/
total 4
drwxr-xr-x. 1 root root 106 Feb  3  2016 2017-05-08-14:29:00
lrwxrwxrwx. 1 root root  19 May  8 18:23 cur -> 2017-05-08-14:29:00
# umount /mnt/wd_backup_disk/

Read only setup for reading snaphosts, new fstab line:

UUID=ee43a6cf-01a8-419a-8e2e-3b995e418995 /mnt/wd_backup_disk btrfs defaults,noauto,subvol=rhea_home_snapshots,ro 0 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment