digint / btrbk

Tool for creating snapshots and remote backups of btrfs subvolumes
https://digint.ch/btrbk/
GNU General Public License v3.0
1.7k stars 123 forks source link

add script for restoring raw backups #401

Closed fr3aker closed 3 years ago

fr3aker commented 3 years ago

Adds a basic python script to restore incremental raw backups into a btrfs subvolume.

The implementation status of this script is mostly "works for me". pbkdf2 and gpg options are not supported. Mileage for (de)compressors may vary. It should be easy enough to extend if anyone needs the missing features.

usage:

btrbk_restore_raw.py -h
usage: btrbk_restore_raw.py [-h] [--ignore-missing] [--openssl-keyfile OPENSSL_KEYFILE] backup btrfs_subvol

restore btrbk raw backup

positional arguments:
  backup                backup file to restore; for incrementalbackups the parent files must be in the same directory
  btrfs_subvol          btrfs subvolume to restore snapshots to using btrfs receive

optional arguments:
  -h, --help            show this help message and exit
  --ignore-missing      do not fail on missing parent snapshots
  --openssl-keyfile OPENSSL_KEYFILE
                        path to private encryption key file

example run with success:

# btrbk_restore_raw.py /mnt/backups/ROOT.20210709.btrfs.xz.encrypted /test --openssl-keyfile /etc/btrbk/openssl_enc.key
2021-07-09 20:58:18,215 INFO - restoring backup ROOT.20210708.btrfs.xz.encrypted
2021-07-09 20:59:54,994 INFO - restoring backup ROOT.20210709.btrfs.xz.encrypted
2021-07-09 20:59:57,229 INFO - finished; restored 2 backup files

example run with error:

# btrbk_restore_raw.py /mnt/backups/ROOT.20210709.btrfs.xz.encrypted /test --openssl-keyfile /etc/btrbk/openssl_enc.key
2021-07-09 21:03:50,464 INFO - restoring backup ROOT.20210708.btrfs.xz.encrypted
2021-07-09 21:03:52,571 ERROR - error running ['btrfs', 'receive', '/test']: At subvol ROOT.20210708
ERROR: creating subvolume ROOT.20210708 failed: File exists
2021-07-09 21:03:52,571 INFO - restoring backup ROOT.20210709.btrfs.xz.encrypted
2021-07-09 21:03:54,582 ERROR - error running ['btrfs', 'receive', '/test']: ERROR: creating snapshot test/ROOT.20210708 -> ROOT.20210709 failed: File exists
2021-07-09 21:03:54,582 INFO - finished; restored 2 backup files
digint commented 3 years ago

Thanks for contributing!

I finally got around to test this a bit, works for me as well.

Pushed some tiny fixups (will squash them on final merge).

I'd love to see a -n, --dryrun option, dumping the commands that will be executed. Would that be possible / not too complicated? Point is that it makes me very nervous when running scripts like this as root, without being able to double-check what will happen.

fr3aker commented 3 years ago

Thank you for the review and fixes! It should require reasonable effort to add a dry run option. I'll look into it.

fr3aker commented 3 years ago

Added the dry run mode. The output contains a small lie: in order to have the file name easily visible it introduces an artificial "cat {file}" as the first command in the pipeline. In reality it simply passes on the file handle to stdin.

digint commented 3 years ago

Great, thanks.

Again added some cosmetics. Unless you yell I'll merge this (squashed) for next release (soon)

fr3aker commented 3 years ago

looks good to me, thanks for checking!

digint commented 3 years ago

merged in: 82860f0f4c413cb73928bf434e0c983cd7a6dd7a, bddd7a1d69ce2ed70cfc6b1c68560393be49ae67

now eagerly waiting for my disks doing raw backups to finally fail :D