btrbk icon indicating copy to clipboard operation
btrbk copied to clipboard

pre-&post-bkup command/script

Open dd-rx opened this issue 11 years ago • 15 comments

hi there :)

first off, thank you for this nifty little tool. makes my backup-workflow easier.

back to the matter: it would be nice to be able to run a command or a script before and/or after the backup is executed. i for example do not have mounted my subvolid=0's by default so for now i put btrbk in a shell script where i first mount the subvolid=0 of the disks needed, run the backups/snapshots and then unmount them again. it works but it's yet another wrapper-script flying around...

bonus would be an action on error.

dd-rx avatar Aug 14 '15 18:08 dd-rx

Thank you for the feedback!

The reason why this is not implemented is security-related: If btrbk runs user-defined commands, it makes it possible for an attacker to run ANY commands as root by altering the config file. I don't like this.

What I thought of are "list-volumes", "list-subvolumes", "list-targets" command line commands, making it possible to write a cron job like this:

for vol in `btrbk list-volumes`; do mount $vol; done
btrbk run
for vol in `btrbk list-volumes`; do umount $vol; done

Maybe I could also introduce a configuration option to include/exclude volumes/subvolumes/targets from the list.

digint avatar Aug 14 '15 22:08 digint

good reason you have there. agree on that.

good solution - especially as it keeps the list up2date and correct instead of me miss-typing stuff. guess that would work for me. the option to in-/exclude would be nice, too.

dd-rx avatar Aug 15 '15 20:08 dd-rx

hey @digint, news on this one? ;-)

dd-rx avatar Sep 04 '15 10:09 dd-rx

Here's the news: I'm currently implementing "labels" for subvolumes, e.g.

volume /mnt/btr_pool
  subvolume rootfs
    label mylabel

this will allow to run btrbk commands on labels only, as is was already possible with subvolumes, e.g. btrbk run mylabel. Additionaly you will be able to list all the subvolumes using btrbk list mylabel.

Also this will allow to implement pre/post hooks, like a btrbk --post-receive myscript.sh run, which will set some environment variables (like BTRBK_SUBVOLUME=/mnt/btr_pool/rootfs and BTRBK_RSH=ssh root@myhost [...]) and run myscript.sh after a successful btrfs receive operation.

This is work in progress, I'll push a branch as soon as there is something useful.

digint avatar Sep 06 '15 09:09 digint

Hi,

could this be used to make a softlink to the latest snapshot? E.g. ln -s "latest snapshot" to latest? So I can rsync the latest snapshot to an mounted ntfs volume.

would be cool

mac-linux-free avatar Sep 06 '15 09:09 mac-linux-free

@mac-linux-free: yes, this is also on the todo-list

digint avatar Sep 06 '15 10:09 digint

Here is what is implemented so far, on branch list-config.

You can group volumes/subvolumes using a new "group" configuration option:

volume /mnt/btr_pool
  subvolume rootfs
    group mygroup
    ssh_identity  /etc/btrbk/ssh/id_ed25519
    target send-receive ssh://example.com/mnt/btr_backup/

Note that grouping does not yet work on target sections, this will be added soon.

For scripting purposes, there are new "list" commands for btrbk:

  • "list-source": list all subvolume statements of the config,
  • "list-target": list all target statements of the config

Both commands dump key="value" lines which can be used (eval'ed) in scipts. Note that "list-source" is necessary if you defined subvolumes without any target (snapshot-only) in the config.

examples:

# btrbk list-source
source_path="/mnt/btr_pool/rootfs" snapshot_path="/mnt/btr_pool" snapshot_basename="rootfs" source_host="" source_rsh=""

# btrbk list-target mygroup
source_path="/mnt/btr_pool/rootfs" snapshot_path="/mnt/btr_pool" snapshot_basename="rootfs" source_host="" source_rsh="" target_path="/mnt/btr_backup" target_host="example.com" target_rsh="/usr/bin/ssh -i /etc/btrbk/ssh/id_ed25519 [email protected]"

An example script could look like this:

btrbk list-target | while read line; do 
  eval $line
  echo "$source_rsh /some/cmd/on/snapshots $snapshot_path/$snapshot_basename.*"
  echo "$target_rsh /some/cmd/on/backup/targets $target_path/$snapshot_basename.*"
done

output of above script:

 /some/cmd/on/snapshots /mnt/btr_pool/rootfs.*
/usr/bin/ssh -i /etc/btrbk/ssh/id_ed25519 [email protected] /some/cmd/on/backup/targets /mnt/btr_backup/rootfs.*

digint avatar Sep 10 '15 11:09 digint

Great news. I will test soon.

I have another question for the future of btrbk: Though my main backupserver is complete on btrfs but some of my servers are not. Would it be possible to have a non-btrfs-source (ssh, rsync, etc.)?

thx a lot.

mac-linux-free avatar Sep 10 '15 12:09 mac-linux-free

Though my main backupserver is complete on btrfs but some of my servers are not. Would it be possible to have a non-btrfs-source (ssh, rsync, etc.)?

This can be done as follows:

First create a btrfs subvolume on the backup server:

# btrfs subvolume create /mnt/btr_backup/myhost_sync

In your daily cron script, prior to running btrbk, use rsync to sync your source, something like:

rsync -a --delete -e ssh myhost.example.com://data/ /mnt/btr_backup/myhost_sync/

Then run btrbk, with myhost configured without any targets as follows:

volume /mnt/btr_backup
  subvolume myhost_sync
    snapshot_name    myhost

    snapshot_preserve_daily    14
    snapshot_preserve_weekly   20
    snapshot_preserve_monthly  all

This will produce daily snapshots /mnt/btr_backup/myhost.20150101, with retention as defined with the snapshot_preserve_* options.

Note that I'm working on grouping and a "list-source" command (#41), which will can further simplify the rsync part. I'll add this on the FAQ as soon as this is finished.

digint avatar Sep 10 '15 12:09 digint

thank you well :+1: - testing over weekend :-)

dd-rx avatar Sep 10 '15 12:09 dd-rx

I merged the list-config branch to master, with slight changes (in order to be more future-proof): d73e3f184b7b3bf17888309f64592e52a4b5e49c

The config can be dumped as follows:

btrbk config dump volume|source|target [filter...]

I marked this feature experimental, as there will probably be (incompatible) enhancements when people start to use it and request more features.

An example for auto-mounting is in the FAQ.

Additionally, I introduced a new --raw-output command-line option, which produces machine-readable output for "(dry)run" and "tree" commands, and helps a lot for further scripting.

Example (print latest snapshot/backup of backup targets "/mnt/btr_backup/data.YYYYMMDD"):

# btrbk --raw-output tree /mnt/btr_backup/data | tail -n1
/mnt/btr_pool/data /mnt/btr_pool/_btrbk_snap/data.20150924 /mnt/btr_backup/data.20150924

@mac-linux-free: regarding the "link latest snapshot" feature: It is impossible to implement this in a consistent way, as it is not always clear what source/target are really the latest. For simple setups it would work, but as soon as you have resume-only setups with multiple instances of btrbk the "latest" links would inevitably get inconsistent. The new --raw-output feature solves this problem by either re-reading btrfs metadata (when used with btrbk tree), or by dumping the output of btrbk run in a machine-readable way. I hope this helps (comments welcome!).

digint avatar Sep 24 '15 14:09 digint

I've put a lot of effort into useful btrbk list output lately, here's an update on this.

Dear Followers: Please try the latest master (currently at: 3f09d2c37fdcf0a843f38c5a73a3930bd1dc4011), and let me know what you think, and yell if you are missing something! There will be a v0.21.0 release soon, and after releasing it's always hard to change things...

All output format can be modified with the format=table|long|raw option.

These commands print the configuration in several views:

  • btrbk list config [filter...]
  • btrbk list source [filter...]
  • btrbk list volume [filter...]
  • btrbk list target [filter...]

These commands resolve relations between source, snapshots and backup subvolumes:

  • btrbk list backups [filter...]
  • btrbk list snapshots [filter...]
  • btrbk list latest [filter...]

Nice Table of filesystem usage:

  • btrbk usage [filter...]

There is also btrbk config print[-all], which is not very useful in my opinion...

This issue will be closed after releasing v0.21.0.

digint avatar Oct 22 '15 16:10 digint

implemented in btrbk-0.21.0

digint avatar Oct 25 '15 19:10 digint

@digint Sorry to drag this up after so long, but I'm still getting the hang of btrfs (and you could say I'm still getting the hang of linux in general). As I don't want the top level volume of the btrfs pool mounted permanently, I wanted to achieve a similar goal to this. However, I'm using systemd, and I want to somehow make the btrbk systemd service mount the top level volume, run the the backup, and then unmount the top level pool afterwards. Would this be possible?

I've read the FAQ, I've read and re-read this Issue, but I'm afraid to say I'm extremely confused. I get the feeling that where I'm getting confused is that here you were talking about running a script as a cron job, whereas I want to do this via systemd. Would it be possible to give me some instructions on how to achieve this, or perhaps include a systemd service file that has this option (to mount then unmount upon completion)?

eccgecko avatar Jun 01 '18 12:06 eccgecko

I'm afraid there is not much information about systemd in the btrbk docs, as I'm not using systemd myself and want to keep btrbk as generic as possible.

"cron jobs" are the traditional (simple) unix way of running periodic tasks, which is being replaced by systemd timer units on most distros (more complex but also more powerful).

What you need to do is a little script which mounts / runs btrbk / unmounts:

#!/bin/sh
mount /my/volume
btrbk run
umount /my/volume

And then create a systemd service unit which runs this script, as well as a timer unit which periodically runs the service (something similar as in contrib/systemd, read the systemd docs for more info about this).

digint avatar Jun 05 '18 09:06 digint