{"id":223,"date":"2023-07-26T10:37:29","date_gmt":"2023-07-26T08:37:29","guid":{"rendered":"https:\/\/www.hackenberger.at\/blog\/?p=223"},"modified":"2023-07-26T10:37:29","modified_gmt":"2023-07-26T08:37:29","slug":"backup-for-btrfs-with-btrbk","status":"publish","type":"post","link":"https:\/\/www.hackenberger.at\/blog\/2023\/07\/26\/backup-for-btrfs-with-btrbk\/","title":{"rendered":"Backup for Btrfs with BtrBk"},"content":{"rendered":"\n<p>For my Linux laptop, I was looking for a replacement of my previous backup solution, which used rsync and was therefore quite slow, as it had to sift through the whole filesystem. As I migrated from ext4 to btrfs during my last OS upgrade, I switched my backup system to use the (quite simple) <a href=\"https:\/\/github.com\/digint\/btrbk\" target=\"_blank\" rel=\"noreferrer noopener\">btrbk<\/a> backup script.<\/p>\n\n\n\n<p>My goals for this are:<\/p>\n\n\n\n<ul><li>Use an encrypted external SSD drive<\/li><li>Run snapshots regularly in the background through cron \/ systemd<\/li><li>Backup the snapshots to the external SSD automatically whenever I dock my laptop<\/li><li>Unmount the external SSD after the backup finished to I can un-dock my laptop<\/li><\/ul>\n\n\n\n<p>I attached an external SSD drive to my screen&#8217;s USB hub to keep things tidy, formatted it using LUKS and btrfs and set the following options to configure btrbk in <em>\/etc\/btrbk\/btrbk.conf<\/em>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">lockfile                   \/var\/lock\/btrbk.lock\n# Snapshots are for locally retained snapshots\nsnapshot_preserve_min   2d\nsnapshot_preserve       14d\n# Target is the actual backup drive\ntarget_preserve_min     no\ntarget_preserve         20d 10w *m\n\n# Backup to external disk mounted on \/media\/backup-usb\nvolume \/media\/btrfs-root\n  # Create snapshots in \/mnt\/btr_pool\/btrbk_snapshots\n  snapshot_dir btrbk_snapshots\n\n  # Target for all subvolume sections:\n  target \/media\/backup-usb\n\n  subvolume @\n  subvolume @home\n<\/pre>\n\n\n\n<p>You can read-up on the various options for snapshot\/backup retention in the btrfs manpage. The other options basically do:<\/p>\n\n\n\n<ul><li>Tell btrbk to create snapshots of the subvolumes <em>@<\/em> and <em>@home<\/em> from the root of by btrfs filesystem mounted at <em>\/media\/btrfs-root<\/em><\/li><li>Use <em>\/media\/backup-usb<\/em> (the external SSD drive) as the target for backing up the snapshots<\/li><\/ul>\n\n\n\n<p>Then I changed the default btrbk systemd unit file to only create the snapshots (triggered through the default btrbk.timer unit). <\/p>\n\n\n\n<p><em>\/etc\/systemd\/system\/btrbk.service.d\/override.conf<\/em> edited using <em>sudo systemctl edit btrbk.service<\/em>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[Service]\nExecStart=\/usr\/bin\/btrbk snapshot\n<\/pre>\n\n\n\n<p>And I added a mount point for the external drive in <em>\/etc\/fstab<\/em>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/dev\/mapper\/luks-24e9cb66-62f4-4382-8bfe-92eae9f8eaba \/media\/backup-usb btrfs subvol=@,acl,noatime 0    1\n<\/pre>\n\n\n\n<p>Then I added an override for the (on the fly generated) mount point service: <em>\/etc\/systemd\/system\/media-backup\\x2dusb.mount.d\/override.conf<\/em> edited using <em>sudo systemctl edit media-backup\\\\x2dusb.mount<\/em>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[Unit]\n# Propagate start\/stop\/restart of the BindsTo service to this one. I.e. unmount the drive after completing the backup\nBindsTo=btrbk-backup.service\n<\/pre>\n\n\n\n<p>And finally created a new systemd unit to run the backup part (as opposed to the snapshot part) of btrbk. <em>\/etc\/systemd\/system\/btrbk-backup.service<\/em>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Starts a 'btrbk resume' run (i.e. puts all snapshots onto the backup volume)\n# after the backup volume has been mounted in the system\n# ATTENTION:\n# Also depends on \/etc\/systemd\/system\/btrbk.service.d\/override.conf setting 'ExecStart=\/usr\/bin\/btrbk snapshot' to only run the snapshot part from a timer\n# And 'systemctl edit media-backup\\\\x2dusb.mount' setting BindsTo=btrbk-backup.service in order to start the backup and unmount the drive afterwards\n\n[Unit]\nDescription=btrbk backup to external disk\nDocumentation=man:btrbk(1)\n# After means If both this and the After service are started, wait for the After service before starting this one\nAfter=media-backup\\x2dusb.mount\n\n[Service]\nType=oneshot\nExecStart=\/usr\/bin\/btrbk resume\n\n[Install]\n# WantedBy means start this service when the WantedBy is started\nWantedBy=media-backup\\x2dusb.mount\n<\/pre>\n\n\n\n<p>After adding the new service you need to run:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ sudo systemctl daemon-reload\n$ sudo systemctl enable btrbk-backup.service\n<\/pre>\n\n\n\n<p>Now you can watch the logs for the relevant services and mount the drive to test it out:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ sudo journalctl -f -u btrbk-backup.service -u media-backup\\x2dusb.mount\n<\/pre>\n\n\n\n<p>It should run the btrbk resume command and unmount the drive straight away. What remains for me is setting up auto-mounting with gnome. I do store the LUKS password in my keyring, but it would be nice to auto-mount the external drive whenever I dock my laptop. Sure I can enable auto-mounting in general in gnome, but that&#8217;s not what I want.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For my Linux laptop, I was looking for a replacement of my previous backup solution, which used rsync and was therefore quite slow, as it had to sift through the whole filesystem. As I migrated from ext4 to btrfs during my last OS upgrade, I switched my backup system to use the (quite simple) btrbk &hellip; <a href=\"https:\/\/www.hackenberger.at\/blog\/2023\/07\/26\/backup-for-btrfs-with-btrbk\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Backup for Btrfs with BtrBk<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"views":1006,"_links":{"self":[{"href":"https:\/\/www.hackenberger.at\/blog\/wp-json\/wp\/v2\/posts\/223"}],"collection":[{"href":"https:\/\/www.hackenberger.at\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hackenberger.at\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hackenberger.at\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hackenberger.at\/blog\/wp-json\/wp\/v2\/comments?post=223"}],"version-history":[{"count":1,"href":"https:\/\/www.hackenberger.at\/blog\/wp-json\/wp\/v2\/posts\/223\/revisions"}],"predecessor-version":[{"id":224,"href":"https:\/\/www.hackenberger.at\/blog\/wp-json\/wp\/v2\/posts\/223\/revisions\/224"}],"wp:attachment":[{"href":"https:\/\/www.hackenberger.at\/blog\/wp-json\/wp\/v2\/media?parent=223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hackenberger.at\/blog\/wp-json\/wp\/v2\/categories?post=223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hackenberger.at\/blog\/wp-json\/wp\/v2\/tags?post=223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}