How To Setup Slack Notifications
1. Create a slack incoming webhook to your preferred slack channel with the following guide: Slack Webhooks
A. Create a Slack App
Specify the name and choose the Slack Workspace where the notification will be sent.
B. Under the Add features and functionality section, click on Incoming Webhooks and activate Incoming Webhooks.
C. Add New Webhook to Workspace and select which channel on your workspace you would like to send the notifications to.
2. Create a bash script to send a webhook notification on your channel and copy the Webhook URL from the Slack App we just created into the SLACK_WEBHOOK_URL portion of the script.
Make sure that your script has the appropriate executable permission (chmod +x /path/to/script.sh)
#!/bin/bash
SLACK_WEBHOOK_URL=https://slack.com ## Set the WEBHOOK URL HERE
sIFS=$IFS
declare -A param
while IFS='=' read -r -d '&' key value; do
[[ -z "$key" ]] && continue;
param["$key"]=$value
done <<<$(cat)"&"
IFS=$sIFS;
###Slack Section Template###
SECTION_TPL='{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "__MESSAGE__"
}
}';
SECTIONS=""
JOB_NAME=${param[name]}
CURRENT_DATE="$(date)"
STATUS=""
addSection() {
_MSG="$1"
_S=""
[[ "" != "$SECTIONS" ]] && _S=","
SECTIONS+=${_S}$( echo "$SECTION_TPL" | sed "s/__MESSAGE__/${_MSG}/" )
}
setStatus(){
case "${param[status]}" in
1)
STATUS='Success'
;;
2)
STATUS='Failed'
;;
3)
STATUS='Aborted'
;;
4)
STATUS='Partially Completed'
;;
esac
}
case "${param[stage]}" in
"onBackupStart")
addSection "Backup Job (${JOB_NAME}) Started at ${CURRENT_DATE}"
;;
"onBackupEnd")
setStatus
addSection "Backup Job (${JOB_NAME}) Finished at ${CURRENT_DATE}"
addSection "Backup Status: ${STATUS}"
;;
*)
exit
;;
esac
SLACK_MSG='{"text": "Jetbackup Slack Notification", "blocks": ['"${SECTIONS}"']}'
curl -X POST -H 'Content-type: application/json' --data "${SLACK_MSG}" "${SLACK_WEBHOOK_URL}"
3. Create a JetBackup Pre and Post Backup Job Hook and specify the path to your slack script.
WHM > JetBackup > Hooks > Create New Hook:
- Hook Name: pre-Backup slack notification/post-Backup slack notification
- Hook Position: pre/post
- Hook Type: Backup
- Backup Jobs: {Specify your preferred backup jobs or leave blank to apply for all backup jobs}
- Hook Script: {Path to the bash script from Part 2}
4. Start Getting Notifications!
Once your Hooks are set up, you will start receiving notifications on the slack channel depending on your script and hook type. In this case, sending notifications for when a backup starts and finishes and the corresponding status of the backup job.
Further Reading:
JetBackup has additional hook types available for your use.
https://docs.jetbackup.com/manual/whm/Hooks/createNewHook.html#hook-type
Feel free to modify the slack notification bash script in part 2 for additional notifications like getting specific alerts for failed accounts, or when a restore or download was successful.
You could also add customizations and advanced formatting to your slack notification layout with the use of slack’s text formatting and Block Kit.
https://api.slack.com/reference/surfaces/formatting
https://api.slack.com/messaging/composing/layouts
Start your FREE trial
of Jetbackup Today!
Get Started Now!
No credit card required.
Install Jetbackup in minutes.
Latest Posts
Categories
Archive
- October 2024
- September 2024
- August 2024
- July 2024
- May 2024
- April 2024
- February 2024
- January 2024
- December 2023
- November 2023
- October 2023
- August 2023
- July 2023
- April 2023
- January 2023
- August 2022
- May 2022
- March 2022
- January 2022
- December 2021
- November 2021
- October 2021
- September 2021
- August 2021
- July 2021
- June 2021
- May 2021
- March 2021
- February 2021
- January 2021
- December 2020
- October 2020
- August 2020
- April 2020
- March 2020
- February 2020
- January 2020
- December 2019
- November 2019
- September 2019
- August 2019
- July 2019
- June 2019
- April 2019
- March 2019
- January 2019
- December 2018
- November 2018
- October 2018
- September 2018
- August 2018
- May 2018
- April 2018
- March 2018
- February 2018
- January 2018
- December 2017
- November 2017