From 0908ee4559f758040583fcf77420a866986ee104 Mon Sep 17 00:00:00 2001 From: kake26 Date: Sat, 25 Sep 2021 23:41:09 +0200 Subject: [PATCH 01/11] Update 'README.md' Created some branches for various types and updated the readme. --- README.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5d1ff1b..26aa5d4 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,13 @@ # Bash-Stash -A collection of console based bash scripts I've written at various times. It may include scripts written in other languages. \ No newline at end of file +A collection of console based bash scripts I've written at various times. It may include scripts written in other languages. + +# console + +These are daily use type console scripts. + +# OpenWrt + +These are designed to be used with OpenWrt for various tasks. + +Please see the read me in each branch for more info. \ No newline at end of file From d3d237030d7fe3eb5a57d0bff08c8e405387d68c Mon Sep 17 00:00:00 2001 From: kake26 Date: Sat, 25 Sep 2021 23:42:02 +0200 Subject: [PATCH 02/11] Update 'README.md' Updated wrong branch fixing --- README.md | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/README.md b/README.md index 26aa5d4..5d1ff1b 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,3 @@ # Bash-Stash -A collection of console based bash scripts I've written at various times. It may include scripts written in other languages. - -# console - -These are daily use type console scripts. - -# OpenWrt - -These are designed to be used with OpenWrt for various tasks. - -Please see the read me in each branch for more info. \ No newline at end of file +A collection of console based bash scripts I've written at various times. It may include scripts written in other languages. \ No newline at end of file From ed880365cbc1da2e27c973cddaab0cfbd542ee0d Mon Sep 17 00:00:00 2001 From: kake26 Date: Sat, 25 Sep 2021 23:44:35 +0200 Subject: [PATCH 03/11] Update 'README.md' updated readme --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5d1ff1b..27c31ab 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ # Bash-Stash -A collection of console based bash scripts I've written at various times. It may include scripts written in other languages. \ No newline at end of file +# OpenWrt + +These are scripts I've used on OpenWrt for various purposes. \ No newline at end of file From 1d6056b61de027ccbf72634378f494dee8ec4530 Mon Sep 17 00:00:00 2001 From: kake26 Date: Sat, 25 Sep 2021 23:56:05 +0200 Subject: [PATCH 04/11] Added backup.bash Added backup.bash from my OpenWrt scripts. --- backup.bash | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 backup.bash diff --git a/backup.bash b/backup.bash new file mode 100644 index 0000000..854b4a7 --- /dev/null +++ b/backup.bash @@ -0,0 +1,21 @@ +#!/bin/bash + +# Another OpenWrt bash script + +# Run backups via restic + +bkpwd="yourbackuppasswordhere" # We are gonna need that + +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 + +# Two dirs I want to keep backed up + +restic -r sftp:restic@192.168.1.113:/home/restic backup /etc +restic -r sftp:restic@192.168.1.113:/home/restic backup /opt/scripts +restic -r sftp:restic@192.168.1.113:/home/restic backup /root +vnstat -i eth1.2 --exportdb > /tmp/vnstat.db +restic -r sftp:restic@192.168.1.113:/home/restic backup /tmp/vnstat.db +rm /tmp/vnstat.db \ No newline at end of file From 1417d71d7c208a7875fbea578561ea98143c5e10 Mon Sep 17 00:00:00 2001 From: kake26 Date: Sat, 25 Sep 2021 23:58:04 +0200 Subject: [PATCH 05/11] band.bash added Bandwidth usage reporting tool. --- band.bash | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 band.bash diff --git a/band.bash b/band.bash new file mode 100644 index 0000000..bde4b48 --- /dev/null +++ b/band.bash @@ -0,0 +1,18 @@ +#!/bin/bash + +# Requires vnstat to be running on target system + +# initial values +mtotal=1000 # monthly total in GB for high end cut off +dmax=40 # daily max usage value +md=`date +%d` # day of the month numerically +cmd=$(($dmax * $md)) # computed max daily +wif="eth1.2" + +# now vnstat stuff, some of this is specific to openwrt + +vnhourly=`vnstat -i $wif -d -s --oneline | awk -F\; '{print $6}'` # total monthly usage +vnmonthly=`vnstat -i $wif -d -s --oneline | awk -F\; '{print $11}'` # daily total usage + + +logger "dom $md cm $cmd vnhourly $vnhourly vnmonthly $vnmonthly"; From d39f56dafceb2bf6f2e9570b5043690a67454be4 Mon Sep 17 00:00:00 2001 From: kake26 Date: Sat, 17 Dec 2022 00:13:34 -0600 Subject: [PATCH 06/11] ntfy support Added ntfy support --- band.bash | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/band.bash b/band.bash index bde4b48..76c7fa6 100644 --- a/band.bash +++ b/band.bash @@ -1,5 +1,4 @@ #!/bin/bash - # Requires vnstat to be running on target system # initial values @@ -7,7 +6,7 @@ mtotal=1000 # monthly total in GB for high end cut off dmax=40 # daily max usage value md=`date +%d` # day of the month numerically cmd=$(($dmax * $md)) # computed max daily -wif="eth1.2" +wif="wan" # now vnstat stuff, some of this is specific to openwrt @@ -16,3 +15,4 @@ vnmonthly=`vnstat -i $wif -d -s --oneline | awk -F\; '{print $11}'` # daily tota logger "dom $md cm $cmd vnhourly $vnhourly vnmonthly $vnmonthly"; +curl -d "dom $md cm $cmd vnhourly $vnhourly vnmonthly $vnmonthly" 192.168.1.168:83/bandmon \ No newline at end of file From feff8400ffd62518e5a84bfa37f9295e99f8d11a Mon Sep 17 00:00:00 2001 From: kake26 Date: Sat, 17 Dec 2022 00:15:03 -0600 Subject: [PATCH 07/11] Ntfy support Added ntfy support --- backup.bash | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/backup.bash b/backup.bash index 854b4a7..5234ee7 100644 --- a/backup.bash +++ b/backup.bash @@ -13,9 +13,13 @@ export RESTIC_PASSWORD="$bkpwd" # Two dirs I want to keep backed up -restic -r sftp:restic@192.168.1.113:/home/restic backup /etc -restic -r sftp:restic@192.168.1.113:/home/restic backup /opt/scripts -restic -r sftp:restic@192.168.1.113:/home/restic backup /root -vnstat -i eth1.2 --exportdb > /tmp/vnstat.db -restic -r sftp:restic@192.168.1.113:/home/restic backup /tmp/vnstat.db -rm /tmp/vnstat.db \ No newline at end of file +/usb/scripts/bin/restic -r sftp:pi@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 +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 +curl -d "Root backed up" 192.168.1.168:83/openwrt_backup +vnstat -i wan --exportdb > /usb/tmp/vnstat.db +/usb/scripts/bin/restic -r sftp:pi@192.168.1.168:/storage/backup/openwrt backup /usb/tmp/vnstat.db +rm /usb/tmp/vnstat.db +curl -d "Vnstat backed up" 192.168.1.168:83/openwrt_backup \ No newline at end of file From 3ccf356c11aa3b1038ebf753c6615ee3f71bf04f Mon Sep 17 00:00:00 2001 From: kake26 Date: Mon, 26 Dec 2022 22:40:13 -0600 Subject: [PATCH 08/11] vnstat fix Configured vnstat to save DB to external USB. Per https://openwrt.org/docs/guide-user/services/network_monitoring/vnstat This was to solve the persistence issue. Now vnstat db should be backed up normally. --- backup.bash | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/backup.bash b/backup.bash index 5234ee7..63963aa 100644 --- a/backup.bash +++ b/backup.bash @@ -19,7 +19,5 @@ curl -d "ETC backed up" 192.168.1.168:83/openwrt_backup 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 curl -d "Root backed up" 192.168.1.168:83/openwrt_backup -vnstat -i wan --exportdb > /usb/tmp/vnstat.db -/usb/scripts/bin/restic -r sftp:pi@192.168.1.168:/storage/backup/openwrt backup /usb/tmp/vnstat.db -rm /usb/tmp/vnstat.db +/usb/scripts/bin/restic -r sftp:pi@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 From 6d893a43a627bfaffb7cc6af187eb288a1b47fb3 Mon Sep 17 00:00:00 2001 From: kake26 Date: Sat, 9 Dec 2023 22:37:59 -0600 Subject: [PATCH 09/11] 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" + From 8ab998e252e49973a602f218d9d247642153f9d9 Mon Sep 17 00:00:00 2001 From: kake26 Date: Sun, 17 Dec 2023 22:29:33 -0600 Subject: [PATCH 10/11] Config support for band.bash --- band.bash | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/band.bash b/band.bash index 3b49f53..a10aa2d 100644 --- a/band.bash +++ b/band.bash @@ -14,12 +14,15 @@ cleanup() { true } +source ./config.bash + # initial values mtotal=1000 # monthly total in GB for high end cut off dmax=40 # daily max usage value md=`date +%d` # day of the month numerically cmd=$(($dmax * $md)) # computed max daily wif="wan" +token=$(read_json "$JSON_FILE" "ntfy" "token") # now vnstat stuff, some of this is specific to openwrt @@ -28,4 +31,5 @@ vnmonthly=`vnstat -i $wif -d -s --oneline | awk -F\; '{print $11}'` # daily tota logger "dom $md cm $cmd vnhourly $vnhourly vnmonthly $vnmonthly"; -curl -d "dom $md cm $cmd vnhourly $vnhourly vnmonthly $vnmonthly" 192.168.1.168:83/bandmon \ No newline at end of file +curl -d "dom $md cm $cmd vnhourly $vnhourly vnmonthly $vnmonthly" 192.168.1.168:83/bandmon +curl -H "Authorization: Bearer $token" -d "dom $md cm $cmd vnhourly $vnhourly vnmonthly $vnmonthly" https://pngpst.net/openwrt_lan \ No newline at end of file From 71ee8a4ba4f594838337e6ec9e03655900ea81a8 Mon Sep 17 00:00:00 2001 From: kake26 Date: Fri, 26 Jan 2024 17:39:04 -0600 Subject: [PATCH 11/11] Fixing some data that was left in --- commando.bash | 20 ++++++++++++++++++++ config.bash | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 commando.bash diff --git a/commando.bash b/commando.bash new file mode 100644 index 0000000..c05bf3d --- /dev/null +++ b/commando.bash @@ -0,0 +1,20 @@ +#!/bin/bash + +# commando.bash + +set -eo pipefail # you cann add u into -eo, but it drives me nuts so I won't + +if [[ -n "${BASHD_DEBUG}" ]]; then # aievitability 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 + +token=$(read_json "$JSON_FILE" "ntfy" "token") \ No newline at end of file diff --git a/config.bash b/config.bash index 8c6ff54..4e5a9bf 100644 --- a/config.bash +++ b/config.bash @@ -31,7 +31,7 @@ write_json() { 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() {