From 6d893a43a627bfaffb7cc6af187eb288a1b47fb3 Mon Sep 17 00:00:00 2001 From: kake26 Date: Sat, 9 Dec 2023 22:37:59 -0600 Subject: [PATCH] Copied code from the bash framework and add json based config file support --- backup.bash | 30 ++++++++++++++++++++---------- band.bash | 13 +++++++++++++ config.bash | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 10 deletions(-) create mode 100644 config.bash diff --git a/backup.bash b/backup.bash index 63963aa..7d291ad 100644 --- a/backup.bash +++ b/backup.bash @@ -1,23 +1,33 @@ #!/bin/bash -# Another OpenWrt bash script +set -eo pipefail # you cann add u into -eo, but it drives me nuts so I won't + +if [[ -n "${BASHD_DEBUG}" ]]; then # a inevitability that this will be used + set -x +fi + +trap cleanup EXIT # A little more robust cleanup + +cleanup() { + # We can clean up any temp files or what nots, but for now a place holder + true +} + +source ./config.bash # Run backups via restic -bkpwd="yourbackuppasswordhere" # We are gonna need that +bkpwd=$(read_json "$JSON_FILE" "restic" "password") export RESTIC_PASSWORD="$bkpwd" -# resticbasecmd = "restic -r sftp:restic@192.168.1.113:/home/restic backup" # root ssh key must be setup to access the remote target -# OpenWrt uses dropbear so use dropbear's key gen +# Stuff to backup -# Two dirs I want to keep backed up - -/usb/scripts/bin/restic -r sftp:pi@192.168.1.168:/storage/backup/openwrt backup /etc +/usb/scripts/bin/restic -r sftp:nbak@192.168.1.168:/storage/backup/openwrt backup /etc curl -d "ETC backed up" 192.168.1.168:83/openwrt_backup -/usb/scripts/bin/restic -r sftp:pi@192.168.1.168:/storage/backup/openwrt backup /usb/scripts +/usb/scripts/bin/restic -r sftp:nbak@192.168.1.168:/storage/backup/openwrt backup /usb/scripts curl -d "Scripts backed up" 192.168.1.168:83/openwrt_backup -/usb/scripts/bin/restic -r sftp:pi@192.168.1.168:/storage/backup/openwrt backup /root +/usb/scripts/bin/restic -r sftp:nbak@192.168.1.168:/storage/backup/openwrt backup /root curl -d "Root backed up" 192.168.1.168:83/openwrt_backup -/usb/scripts/bin/restic -r sftp:pi@192.168.1.168:/storage/backup/openwrt backup /usb/vnstat/vnstat.db +/usb/scripts/bin/restic -r sftp:nbak@192.168.1.168:/storage/backup/openwrt backup /usb/vnstat/vnstat.db curl -d "Vnstat backed up" 192.168.1.168:83/openwrt_backup \ No newline at end of file diff --git a/band.bash b/band.bash index 76c7fa6..3b49f53 100644 --- a/band.bash +++ b/band.bash @@ -1,6 +1,19 @@ #!/bin/bash # Requires vnstat to be running on target system +set -eo pipefail # you cann add u into -eo, but it drives me nuts so I won't + +if [[ -n "${BASHD_DEBUG}" ]]; then # a inevitability that this will be used + set -x +fi + +trap cleanup EXIT # A little more robust cleanup + +cleanup() { + # We can clean up any temp files or what nots, but for now a place holder + true +} + # initial values mtotal=1000 # monthly total in GB for high end cut off dmax=40 # daily max usage value diff --git a/config.bash b/config.bash new file mode 100644 index 0000000..8c6ff54 --- /dev/null +++ b/config.bash @@ -0,0 +1,45 @@ +#!/bin/bash + +JSON_FILE="config.json" + +read_json() { + local JSON_FILE="$1" + local SECTION="$2" + local KEY="$3" + + jq -r ".${SECTION}.${KEY}" "$JSON_FILE" +} + +# Function to write a value to a JSON configuration file +write_json() { + local JSON_FILE="$1" + local SECTION="$2" + local KEY="$3" + local VALUE="$4" + + # It important to check if the file is valid otherwise stuff breaks + if ! jq -e . < "$JSON_FILE" > /dev/null 2>&1; then + echo "Error: JSON file is not valid JSON." + return 1 + fi + + # Using jq to set the value. The original file is replaced with a new file with the updated value. + jq ".${SECTION}.${KEY} = \"$VALUE\"" "$JSON_FILE" > "temp.json" && mv "temp.json" "$JSON_FILE" + +} + +create_json() { + local JSON_FILE="$1" + echo '{"bashini": "new"}' | jq '.' > "$JSON_FILE" # well we need something in there + write_json "$JSON_FILE" "restic" "password" "backupsarefun" # for the openwrt backup script here for testing +} + +check_json() { + local JSON_FILE="$1" + if [ ! -f "$JSON_FILE" ]; then + create_json "$JSON_FILE" + fi +} + +check_json "$JSON_FILE" +