Skip to content

Instantly share code, notes, and snippets.

@supersonictw
Last active July 30, 2025 05:33
Show Gist options
  • Save supersonictw/1f145442edee81f9c7a98a3b51f878e3 to your computer and use it in GitHub Desktop.
Save supersonictw/1f145442edee81f9c7a98a3b51f878e3 to your computer and use it in GitHub Desktop.
Execute rustic and sends a notification upon successful completion.
#!/bin/sh
# irustic.sh - Execute rustic and sends a notification upon successful completion.
# SPDX-License-Identifier: MIT (https://ncurl.xyz/s/Kkn2DQsNR)
set -e
# rustic-rs/rustic
# https://github.com/rustic-rs/rustic
DIR_RUSTIC="/root/.config/rustic"
DIR_IRUSTIC="/root/.local/bin"
FILE_RUSTIC_CFG="$DIR_RUSTIC/rustic.toml"
FILE_IRUSTIC_SH="$DIR_IRUSTIC/irustic.sh"
# setup rustic
mkdir -p "$DIR_RUSTIC"
touch "$FILE_RUSTIC_CFG"
chmod 0600 "$FILE_RUSTIC_CFG"
chown root:root "$FILE_RUSTIC_CFG"
# setup irustic
mkdir -p "$DIR_IRUSTIC"
touch "$FILE_IRUSTIC_SH"
chmod 0700 "$FILE_IRUSTIC_SH"
chown root:root "$FILE_IRUSTIC_SH"
# generate password
# "_" prefix is used for marking it's auto generated.
IRUSTIC_PASSWORD="_$(openssl rand -hex 18)"
# setup rustic.toml
tee "$FILE_RUSTIC_CFG" <<EOF
[repository]
repository = "/root/.irustic"
password = "$IRUSTIC_PASSWORD" # openssl rand -hex 18
[forget]
keep-hourly = 20
keep-daily = 14
keep-weekly = 8
keep-monthly = 24
keep-yearly = 10
[[backup.snapshots]]
sources = ["/root"]
[[backup.snapshots]]
sources = ["/home"]
[[backup.snapshots]]
sources = ["/srv"]
[[backup.snapshots]]
sources = ["/opt"]
[[backup.snapshots]]
sources = ["/var/mail"]
[[backup.snapshots]]
sources = ["/var/www"]
EOF
echo "Please run init manually after set rustic.toml up:"
echo "rustic init"
#!/bin/bash
# irustic.sh - Execute rustic and sends a notification upon successful completion.
# SPDX-License-Identifier: MIT (https://ncurl.xyz/s/Kkn2DQsNR)
set -Eeuo pipefail
# Setup irustic crontab:
# crontab -e
# 0 */2 * * * /root/.local/bin/irustic.sh >/dev/null 2>&1 # execute silently every 2 hours
# define notify destination
WEBHOOK_DISCORD="https://discordapp.com/api/webhooks/..."
# define notify function
NOTIFY() {
local MESSAGE="$1"
local DATE_NOW="$(date)"
curl \
-X POST \
-H "Content-Type: application/json" \
-d "{\"content\":\"iRustic has been executed at \`$DATE_NOW\`:\n$MESSAGE\"}" \
"$WEBHOOK_DISCORD"
}
# trap 'RAISE $LINENO' ERR
RAISE() {
local MESSAGE="\`$0\` exits with code \`$1\` on line number \`$2\`"
NOTIFY "$MESSAGE"
exit "$1"
}
# capture errors
trap 'RAISE "$?" "$LINENO"' ERR
# run the task
rustic backup
# run the notify
NOTIFY "Successful"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment