garethgeorge / backrest

Backrest is a web UI and orchestrator for restic backup.
GNU General Public License v3.0
930 stars 33 forks source link

Hook - Command - How to call a bash script? #387

Closed mrcsrplln closed 1 month ago

mrcsrplln commented 1 month ago

Hi, firstly thank you for your effort with Backrest, I am really finding it extremely useful. I am having an "issue", probably due to my still limited knowledge of Linux.

Basic Info

What is the "issue"?

What have I tried so far?

If I run the script directly from shell in Debian it works fine and I get my telegram notification.

Any suggestions would be greatly appreciated.

stefanomarty commented 1 month ago

I had a similar problem using a hook with Backrest running in a Docker container. The main issue was that even if you make the script available on a volume, the entire script runs inside the container. Therefore, you need to make all the resources required by the script available inside the container as well.

Since there were no dependencies between the two, I decided to use cron to schedule the operation a few minutes before the Backrest job.

mrcsrplln commented 1 month ago

I had a similar problem using a hook with Backrest running in a Docker container. The main issue was that even if you make the script available on a volume, the entire script runs inside the container. Therefore, you need to make all the resources required by the script available inside the container as well.

Since there were no dependencies between the two, I decided to use cron to schedule the operation a few minutes before the Backrest job.

Thanks. Yes that is a possible solution to be informed when a backup operation starts. But how do you tell if a backup actually completed or if it failed with an error?

stefanomarty commented 1 month ago

But how do you tell if a backup actually completed or if it failed with an error?

Well... I'd probably use a Backrest hook :-) Just kiddin...

A long time ago I would have used an rpc call, but today I find it easier and more straightforward to use a webhook. You can install the server easily from here: https://github.com/adnanh/webhook

Then you just need to configure the hook on the host server to launch your script, passing it all the necessary parameters. You can also put a reverse proxy in front of it and discover that many interesting things can be done with webhooks!

mrcsrplln commented 1 month ago

But how do you tell if a backup actually completed or if it failed with an error?

Well... I'd probably use a Backrest hook :-) Just kiddin...

A long time ago I would have used an rpc call, but today I find it easier and more straightforward to use a webhook. You can install the server easily from here: https://github.com/adnanh/webhook

Then you just need to configure the hook on the host server to launch your script, passing it all the necessary parameters. You can also put a reverse proxy in front of it and discover that many interesting things can be done with webhooks!

Thanks for the hint!! Will surely give it a go!

stefanomarty commented 1 month ago

Great, if you need any hint just send me a msg here, I'll be happy to help! Ciao.

garethgeorge commented 1 month ago

Thanks @stefanomarty for the ideas -- using backrest w/webhook is an interesting setup, aiming to take a look at the project in the evening.

Just dropping in to add that you can use patterns like https://garethgeorge.github.io/backrest/cookbooks/command-hook-examples#notify-a-healthcheck-service to include properties from the hook in the webhook calls you issue using curl .

Fwiw if you're trying to configure telegram you might look at Backrest's shoutrrr integration which offers telegram support https://containrrr.dev/shoutrrr/v0.8/services/telegram/

mrcsrplln commented 1 month ago

As suggested by @stefanomarty , to solve the issue I used webhooks. So, basically:

  1. Backrest calls (via curl) a hook provided by webhook (as systemd service), also passing specific parameters (e.g. disk name, status of snapshot etc...)
  2. The hook calls a bash script, passing it the parameters received via curl
  3. The bash script executes Apprise, which sends me a Telegram notification with a message containing the parameters set at point 1
  4. I get my notifications when my backups misbehave...

Thanks again @stefanomarty for the support, I hadn't worked with webhooks before, this opens a lot of opportunities. Also thanks again to @garethgeorge for the amazing work with backrest.