Created
January 10, 2022 01:49
-
-
Save Annath/57bbf4392eee139216e0ceb74f426946 to your computer and use it in GitHub Desktop.
Quick and dirty backup script for a DD-WRT router with Twilio SMS notifications if it fails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Config variables supplied by the user | |
TWILIO_ACCOUNT_SID="" | |
TWILIO_AUTH_TOKEN="" | |
TWILIO_NUMBER="" | |
TO_NUMBER="" | |
LOCAL_BACKUP_PATH="/path/to/router-backups" | |
ROUTER_HOSTNAME="192.168.1.1" | |
SSH_HOST="root@${ROUTER_HOSTNAME}" | |
SSH_OPTIONS="-q $SSH_HOST" | |
send_sms() { | |
curl -X POST \ | |
-d "Body=$1" \ | |
-d "From=$TWILIO_NUMBER" \ | |
-d "To=$TO_NUMBER" \ | |
"https://api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/Messages" \ | |
-u "$TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN" | |
} | |
write_syslog() { | |
logger -t router-backup -p user.error $1 | |
} | |
( | |
set -e | |
BACKUP_DIR="/tmp/root" | |
BACKUP_FILE_PATH="${BACKUP_DIR}/nvrambak_$(date -I).bin" | |
write_syslog "Deleting $BACKUP_FILE_PATH on router" | |
ssh $SSH_OPTIONS "rm -f $BACKUP_FILE_PATH" | |
write_syslog "Backup up router to $BACKUP_FILE_PATH" | |
ssh $SSH_OPTIONS "nvram backup $BACKUP_FILE_PATH" | |
write_syslog "Downloading router:${BACKUP_FILE_PATH} to ${LOCAL_BACKUP_PATH}" | |
scp ${SSH_OPTIONS}:${BACKUP_FILE_PATH} ${LOCAL_BACKUP_PATH} | |
) || { | |
write_syslog "Failed to create router backup" | |
send_sms 'Your router backup stopped working. Go fix it' | |
exit 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment