1
1
Fork 0

cleaned up stale files and updated README

This commit is contained in:
david 2023-10-27 11:43:36 +02:00
parent 99539d54aa
commit 9d6c642019
9 changed files with 48 additions and 676 deletions

View File

@ -2,9 +2,19 @@
My personal collection of handy scripts. mostly bash.
- [restic_backup.sh](#restic_backup.sh)
- [restic-backup.sh](#restic-backup.sh)
- [restic-repos.sh](#restic-repos.sh)
- [btrfs-snapshots.sh](#btrfs-snapshots.sh)
- [odroid-fancontrol.sh](#odroid-fancontrol.sh)
## restic_backup.sh
More:
- [lib.sh](#lib.sh)
- [template.sh](#template.sh)
---
## restic-backup.sh
> This script is my very simple take on how to perform restic backups.
@ -23,7 +33,7 @@ In the config section at the top of the script you can change various options to
PASSWORD="/root/.restic-password"
REPO="sftp:<remote-server>:/path/to/repository"
KEEP=30
BIN="/usr/bin/restic"
BIN="/usr/bin/restic"
OPTIONS="-p $PASSWORD -r $REPO -q"
```
@ -48,3 +58,38 @@ or using cron.
```
Happy backuping! :-)
## restic-repos.sh
## btrfs-snapshots.sh
## odroid-fancontrol.sh
## lib.sh
## template.sh
Use this as a base for new scripts, it already has the essentials.
```sh
# Load lib.sh
SCRIPT_LIB="lib.sh"
SCRIPT_PATH=$(readlink -f "$0")
SCRIPT_DIR=$(dirname "$SCRIPT_PATH")
if [[ -r "$SCRIPT_DIR/$SCRIPT_LIB" ]]; then
source "$SCRIPT_DIR/$SCRIPT_LIB"
lib_print "Loading version $LIB_GREEN_BOLD$LIB_VERSION$LIB_CLEAR of $LIB_GREEN_BOLD$LIB_NAME$LIB_CLEAR"
else
echo "Error: Cannot load library file [$SCRIPT_DIR/$SCRIPT_LIB]"
exit 1
fi
```
-----
## watch-containers.sh
Simple healthchecks script to monitor running containers.
## switch-desktop.sh
I think this is only useful with ElementaryOS. I use(d) this with a hot corner to control desktops with the mouse.

View File

@ -1,74 +0,0 @@
#!/bin/sh
# simple script to clone a virtual machine (vmware)
# to another datastore/folder.
# usage: ./clone_vm.sh <source path> <destination path>
# source path must be a directory and contain a virtual machine (at least a single .vmdk file)
# destination path must a directory, preexist and be empty (to avoid overwriting another machine)
# sources:
# https://serverfault.com/questions/372526/move-vmware-esxi-vm-to-new-datastore-preserve-thin-provisioning
# https://communities.vmware.com/thread/498408
echo; echo "**** Simple virtual machine cloning script ****"; echo
sleep 0.5; echo "Before you continue all snapshots need to be deleted"
echo "and all virtual disks (.vmdk files) need to be consolidated."; echo
echo "And please, for your own sake, make sure the VM is powered down!"; echo
echo "Do you want to continue? [Y]"
read a
if [ -z $a ] || [ $a == "Y" ] || [ $a == "y" ]; then
if [ $1 ] && [ $2 ]; then
if [ -d $1 ]; then
if [ -r $1 ]; then
if [ -n "$(ls -A $1)" ]; then
source=${1%/}
if [ -d $2 ]; then
if [ -w $2 ]; then
if [ -z "$(ls -A $2)" ]; then
destination=${2%/}
sleep 0.5; echo "INFO: Preflight checks good. Commencing cloning process."
for disk in $(find "$source" -maxdepth 1 -type f | grep ".vmdk" | grep -v "flat.vmdk"); do
if [ -n "${disk##*delta*}" ]; then
echo "ERROR: You little rebel did not remove all snapshots. Failsave exit!"; exit 1
fi
#try the following to move a specific snapshot
#find . -maxdepth 1 -type f | grep "000030.vmdk"
sleep 0.5; echo "INFO: Cloning $disk to $destination"
vmkfstools -i $disk -d thin $destination/${disk##*/}
if [ $? -ne 0 ]; then
echo "ERROR: An error occured while cloning $disk to $destination."
fi
done
for file in $(find "$source" -maxdepth 1 -type f | grep -v ".vmdk"); do
sleep 0.5; echo "INFO: Copying $file to $destination"
cp "$file" "$destination"
if [ $? -ne 0 ]; then
echo "ERROR: An error occured while copying $file to $destination."
fi
done
else
sleep 0.5; echo "ERROR: Destination path is not empty ($2)."; exit 1
fi
else
sleep 0.5; echo "ERROR: Destination path is not writeable ($2)."; exit 1
fi
else
sleep 0.5; echo "ERROR: Destination path is not a directory ($2)."; exit 1
fi
else
sleep 0.5; echo "ERROR: Source path empty ($1)."; exit 1
fi
else
sleep 0.5; echo "ERROR: Source path is not readable ($1)."; exit 1
fi
else
sleep 0.5; echo "ERROR: Source path is not a directory ($1)."; exit 1
fi
else
sleep 0.5; echo "ERROR: Source and destination path must be given."; exit 1
fi
else
sleep 0.5; echo "ERROR: Mission aborted by user."
exit 2
fi

View File

@ -1,72 +0,0 @@
#!/bin/bash
#
# Odroid HC4 fan control script
#
# Information (don't touch unless you're me)
AUTHOR="david@socialnerds.org"
VERSION="0.2.1"
LICENSE="GPLv3"
# Configuration
THRESHOLD=46000
DISTANCE=1000
INTERVAL=30
STEPS=(120 160 210 255)
FAN="/sys/class/hwmon/hwmon2/pwm1"
LOGFILE="/var/log/hc4_fancontrol.log"
# Get all temperature readings and return the highest value
function get_temp() {
local TEMPS=($(cat /sys/devices/virtual/thermal/thermal_zone*/temp))
local RESULT=${TEMPS[0]};
for TEMP in ${TEMPS[@]}; do
if [ $TEMP -gt $RESULT ]; then
RESULT=$TEMP
fi
done
echo $RESULT
}
# Get current fan speed
function get_speed() {
local SPEED=$(cat $FAN)
echo $SPEED
}
# Set new fan speed
function set_speed() {
local CURRENT_SPEED=$(get_speed)
local NEW_SPEED=$1
if [ $CURRENT_SPEED -ne $NEW_SPEED ]; then
echo $NEW_SPEED > $FAN
if [ -n $LOGFILE ]; then
echo "$(date +%Y%m%d-%H%M%S): Temp[$(get_temp)], Speed[$(get_speed)]" >> $LOGFILE
fi
sleep 3
fi
}
# Set fan mode to manual
echo "disabled" | tee /sys/class/thermal/thermal_zone{0,1}/mode > /dev/null
# Run loop
while true; do
TEMP=$(get_temp)
SPEED=$(get_speed)
NEW_THRESHOLD=$THRESHOLD
if [ $TEMP -gt $THRESHOLD ]; then
for STEP in ${STEPS[@]}; do
if [ $TEMP -gt $NEW_THRESHOLD ]; then
NEW_THRESHOLD=$((NEW_THRESHOLD + DISTANCE))
if [ $SPEED -lt $STEP ]; then
set_speed $STEP
fi
fi
done
else
set_speed 0
fi
sleep $INTERVAL
done

View File

@ -1,28 +0,0 @@
#!/bin/bash
if [ -z "$1" ]; then
echo "error: pls supply at least one domain name"
exit 1
fi
NAME="$1"
DOMAINS="$*"
OPTIONS="--issue --dns dns_miab --server letsencrypt"
COMMAND="/usr/bin/acme.sh"
ACME_PATH="/srv/acme"
for DOMAIN in $DOMAINS; do
OPTIONS="$OPTIONS -d $DOMAIN"
done
# requesting certificate
$COMMAND $OPTIONS
# installing certificate to $ACME_PATH
if [ $? -eq 0 ]; then
$COMMAND --install-cert -d $NAME --key-file $ACME_PATH/$NAME.key --fullchain-file $ACME_PATH/$NAME.crt --reloadcmd "systemctl restart nginx.service"
else
echo "error: an error occured while issuing certificate."
exit 1
fi

View File

@ -1,229 +0,0 @@
#!/bin/bash
#
# Repository/user management script for Restics's REST server"
#
# Information
RR_NAME="rest-repos.sh"
RR_DESCRIPTION="Repository/user management script for Restic's REST server"
RR_VERSION="0.1.0"
RR_AUTHOR="david@socialnerds.org"
RR_LICENSE="GPLv3"
# Configuration
RR_DATA_PATH="/srv/backup/rest-server"
RR_NGINX_PATH="/etc/nginx/rest-server.conf.d"
RR_NGINX_TEMPLATE="$RR_NGINX_PATH/rest-server.conf.template"
RR_LOG_FILE="/var/log/rest-repos.log"
RR_HOST="backup.socialnerds.org"
RR_PROTOCOL="https"
# Initialization
RR_DEPS="htpasswd tr head systemctl find awk sed sort"
# Text formatting
CLEAR="\e[0m"
# Text settings.
BOLD="\e[1m"
UNDERLINE="\e[4m"
# Text color.
RED="\e[31m"
GREEN="\e[32m"
YELLOW="\e[33m"
BLUE="\e[34m"
MAGENTA="\e[35m"
CYAN="\e[36m"
# Text color with bold font.
RED_BOLD="\e[1;31m"
GREEN_BOLD="\e[1;32m"
YELLOW_BOLD="\e[1;33m"
BLUE_BOLD="\e[1;34m"
MAGENTA_BOLD="\e[1;35m"
CYAN_BOLD="\e[1;36m"
# Background color.
RED_BG="\e[41m"
GREEN_BG="\e[42m"
YELLOW_BG="\e[43m"
BLUE_BG="\e[44m"
MAGENTA_BG="\e[45m"
CYAN_BG="\e[46m"
# Background color with bold font.
RED_BG_BOLD="\e[1;41m"
GREEN_BG_BOLD="\e[1;42m"
YELLOW_BG_BOLD="\e[1;43m"
BLUE_BG_BOLD="\e[1;44m"
MAGENTA_BG_BOLD="\e[1;45m"
CYAN_BG_BOLD="\e[1;46m"
# Functions
function rr_output() {
# Print text/messages to screen
if [[ "$1" =~ ^\! ]]; then
printf "$RED%b$CLEAR\n" "${1#\!}"
else
if [[ "$RR_Q" -ne 1 ]]; then
printf "%b\n" "$1"
fi
fi
}
function rr_help() {
# Print help information
rr_output "$RR_DESCRIPTION\n\nUsage:\n$RR_NAME <options> <repo>\n\nOptions:
-l, --list\t\tList repositories
-r, --remove\t\tRemove repository
-h, --help\t\tHelp screen
-v, --version\t\tVersion information
-q, --quiet\t\tNo output except errors"
exit 0
}
function rr_version() {
# Print version information
rr_output "Version: $BOLD$RR_VERSION$CLEAR\nAuthor: $RR_AUTHOR\nLicense: $RR_LICENSE"
exit 0
}
function rr_passwd() {
# Generate random password
LC_ALL=C tr -dc 'a-zA-Z0-9' < /dev/urandom | head -c 16; echo
}
function rr_list() {
# List all existing repositories
for RR_REPO in $(find $RR_DATA_PATH -maxdepth 1 -mindepth 1 -type d -printf "%f\n" | sort); do
local RR_SIZE=$(du -s -h $RR_DATA_PATH/$RR_REPO | awk '{print $1}')
if [[ -f $RR_NGINX_PATH/$RR_REPO.conf && -f $RR_NGINX_PATH/$RR_REPO.htpasswd ]]; then
rr_output "$BOLD$RR_REPO$CLEAR[$RR_SIZE]"
else
local RR_DISABLED="$RR_DISABLED $RR_REPO[$RR_SIZE]"
fi
done
for RR_REPO in $RR_DISABLED; do
rr_output "$RR_REPO"
done
}
function rr_add() {
# Enable or create a repository
if [ -n "$1" ]; then
if [[ ! -d "$RR_DATA_PATH/$1" ]]; then
mkdir "$RR_DATA_PATH/$1"
chown rest-server:rest-server "$RR_DATA_PATH/$1"
chmod 700 "$RR_DATA_PATH/$1"
fi
if [[ ! -f "$RR_NGINX_PATH/$1.conf" && ! -f "$RR_NGINX_PATH/$1.htpasswd" ]]; then
sed "s/RR_REPO/$1/g" $RR_NGINX_TEMPLATE > "$RR_NGINX_PATH/$1.conf"
RR_PASSWD="$(rr_passwd)"
htpasswd -n -b "$1" "$RR_PASSWD" > "$RR_NGINX_PATH/$1.htpasswd"
rr_output "Repository has been added [$1]\nRepository URL:$BOLD rest:$RR_PROTOCOL://$1:$RR_PASSWD@$RR_HOST/$1/$CLEAR"
else
rr_output "!Repository does already exist and is enabled\nTry removing it first with -r or --remove"
exit 1
fi
fi
}
function rr_remove() {
# Remove a repository
if [[ -n "$1" ]]; then
rm $RR_NGINX_PATH/$1.conf &>/dev/null
rm $RR_NGINX_PATH/$1.htpasswd &>/dev/null
if [[ -d $RR_DATA_PATH/$1 ]]; then
rr_output "Repository has been removed [$1]\nThe data needs to be deleted manually [$RR_DATA_PATH/$1]"
else
rr_output "!Repository does not exist [$1]"
exit 1
fi
fi
}
# Preflight
for RR_DEP in $RR_DEPS; do
if ! $(command -v $RR_DEP &>/dev/null); then
RR_MISS="$RR_MISS, $RR_DEP"
fi
done
if [[ -n $RR_MISS ]]; then
rr_output "!One or more missing dependencies [${RR_MISS#, }]\nTry installing them with your package manager"
exit 1
fi
#TODO: check if nginx path and data path exists and both are writeable
# Option handler
while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do
case $1 in
-v|--version)
RR_V=1
;;
-h|--help)
RR_H=1
;;
-l|--list)
RR_L=1
;;
-r|--remove)
RR_R=1
;;
-q|--quiet)
RR_Q=1
;;
*)
rr_output "!Unknown option [$1]\nTry --help or -h for available options"
exit 1
;;
esac
shift
done
if [[ "$1" == '--' ]]; then
shift
fi
if [[ $RR_L -eq 1 ]]; then
rr_list
exit 0
elif [[ $RR_H -eq 1 ]]; then
rr_help
exit 0
elif [[ $RR_V -eq 1 ]]; then
rr_version
exit 0
fi
if [[ -z "$1" ]]; then
rr_list
exit 0
else
if [[ "$1" =~ ^[a-zA-Z0-9_-]{3,20}$ ]]; then
if [[ $RR_R -eq 1 ]]; then
rr_remove "$1"
else
rr_add "$1"
fi
# Reloading Nginx after config changes
systemctl reload nginx.service
else
rr_output "!Invalid repository name [$1]\nMust be an alphanumeric string between 3 and 20 characters"
exit 1
fi
fi
# Here be dragons
exit 0

View File

@ -1,81 +0,0 @@
#!/bin/bash
# name: restic_backup.sh
# version: 0.1
# author: david@socialnerds.org
# license: MIT
# description: a simple script to automate the restic backup utility.
# i created this for my personal use, though you are very
# welcome to reuse and improve.
# **** CONFIG ****
PASSWORD="/root/.restic-password"
REPO="sftp:remote-server.tld:/path/to/repository"
KEEP=30
BIN="/usr/bin/restic"
OPTIONS="-q"
# **** START ****
CMD="$BIN -p $PASSWORD -r $REPO $OPTIONS"
#TODO: check for elevated privileges
# Generate a new password file if it is missing or empty
if [ ! -s $PASSWORD ]; then
#echo "error: Password file is empty or does not exist. [$PASSWORD]"
#exit 1
tr -cd '[:alnum:]' < /dev/urandom | fold -w32 | head -n1 > $PASSWORD
chmod 600 $PASSWORD
echo "warning: New password file generated since it does not exist or is empty. [$PASSWORD]"
fi
# Initialize a new repo if the repo url has no config file
$CMD snapshots >/dev/null 2>&1
if [ $? -ne 0 ]; then
$CMD init >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "error: A problem occured while creating a new repository. [$REPO]"
exit 1
fi
echo "warning: New repository initialized. [$REPO]"
fi
# Run the restic repository check
$CMD check > /dev/null
if [ $? -ne 0 ]; then
echo "error: Repository check failed. [$REPO]"
exit 1
fi
# First option must be a readable folder/file
if [ $1 ]; then
if [ -r $1 ]; then
LOCAL=$1
else
echo "error: The path given does not seem to be readable. [$1]"
exit 1
fi
else
echo "error: Local path must be given as an option."
exit 1
fi
# Run the actual backup
$CMD backup ${@:2} $LOCAL > /dev/null
if [ $? -ne 0 ]; then
echo "error: Something went wrong while running backup. [$LOCAL]"
exit 1
fi
# Delete old snapshots
$CMD forget --keep-last $KEEP --path $LOCAL --prune > /dev/null
if [ $? -ne 0 ]; then
echo "error: Something went wrong while deleting old snapshots. [$LOCAL]"
exit 1
fi
# **** END ****
exit 0

View File

@ -1,45 +0,0 @@
#!/bin/bash
# name: switch_desktop.sh
# version: 0.1
# author: david@socialnerds.org
# description: switch to next, previous, specific desktop.
# you can also toggle between first and second desktop.
#
# usage: ./switch_desktop.sh next|prev|<int>|toggle
num=$(wmctrl -d | grep "*" | awk '{print $1}')
lockfile="/tmp/switch_desktop.lock"
if [ ! -f $lockfile ]; then
echo "creating lockfile [$lockfile]"
touch $lockfile
if [[ "$1" =~ ^[0-9]+$ ]]; then
echo "switching to specific desktop [$1]"
wmctrl -s $1
elif [ "$1" == "next" ]; then
echo "switching to next desktop [$next]"
next=$((num+1))
wmctrl -s $next
elif [ "$1" == "prev" ]; then
if [ $num -ne 0 ]; then
echo "switching to previous desktop [$prev]"
prev=$((num-1))
wmctrl -s $prev
fi
elif [ "$1" == "toggle" ]; then
if [ $num -eq 0 ]; then
echo "switching to second desktop"
wmctrl -s 1
else
echo "switching back to first desktop"
wmctrl -s 0
fi
fi
echo "waiting for 0.5 seconds"
sleep 0.5
echo "cleaning up lockfile [$lockfile]"
rm $lockfile
fi

View File

@ -1,54 +0,0 @@
#!/bin/bash
# this will take read-only btrfs snapshots and make it available in /srv/snapshots
# variables
SUBVOLUMES="/"
DESTINATION_ROOT=/srv/snapshots
TIMESTAMP=$(date +%Y%m%d%H%M) #add %S if you want sub-minute snapshots
PREFIX="snapshot-"
NAME="$PREFIX$TIMESTAMP"
MAX_SNAPSHOTS=28 #how many snapshots should be kept?
# preflight checks
if [ $(whoami) != "root" ]; then
echo "error: you need to be root"
exit 1
fi
# take the snapshots
for SUBVOLUME in $SUBVOLUMES; do
if [ -z ${SUBVOLUME#/} ]; then
SNAPSHOT_PATH="$DESTINATION_ROOT"
else
PATH="$DESTINATION_ROOT/${SUBVOLUME#/}"
fi
if [ ! -d $SNAPSHOT_PATH ]; then
echo "warning: snapshot path ($SNAPSHOT_PATH) does not yet exist. creating."
mkdir -p $SNAPSHOT_PATH
fi
if [ -d $SNAPSHOT_PATH/$NAME ]; then
echo "warning: snapshots with same name already exists. skipping."
else
#TODO: handle failed snapshots and failed link creations
btrfs -q subvolume snapshot -r $SUBVOLUME $SNAPSHOT_PATH/$NAME
if [ -h $SNAPSHOT_PATH/latest ]; then
rm $SNAPSHOT_PATH/latest
fi
ln -sf $SNAPSHOT_PATH/$NAME $SNAPSHOT_PATH/latest
fi
done
# prune old snapshots
SNAPSHOTS=$(ls -r $SNAPSHOT_PATH | grep $PREFIX)
i=0
for SNAPSHOT in $SNAPSHOTS; do
if [ $i -ge $MAX_SNAPSHOTS ]; then
#TODO: handle failed snapshot deletions
btrfs -q subvolume delete $SNAPSHOT_PATH/$SNAPSHOT
fi
i=$((i+1))
done
# end script successfully
exit 0

View File

@ -1,90 +0,0 @@
#!/bin/bash
# name: watch_containers.sh
# author: david@socialnerds.org
# version: 0.1
# license: GPLv3
# description: Checks if the running container count
# matches a number provided as $1 and optionally
# reports results to a healthchecks endpoint.
# usage: ./watch_containers.sh <number> <optional-healthchecks-url>
# **** function definitions ****
function usage {
echo "usage: ./watch_containers.sh <number> <optional-healthchecks-url>"
}
function get_container_count {
local CNT=$(docker ps --format {{.Names}} | wc -l)
if is_int "$CNT"; then
echo $CNT
fi
}
function is_int {
local INT_EXPR='^[0-9]+$'
if [[ $1 =~ $INT_EXPR ]]; then
return 0
else
return 1
fi
}
function is_url {
local URL_EXPR='^https?://.+$'
if [[ $1 =~ $URL_EXPR ]]; then
return 0
else
return 1
fi
}
# **** start ****
#exit if no options
if [ -z "$1" ]; then
usage
exit 1
fi
#exit if $1 is no intager
if ! is_int "$1"; then
usage
exit 1
fi
#set expected running containers
NUM=$1
#get running container count
CNT=$(get_container_count)
#set healthchecks url if not empty
URL=""
if [ -n "$2" ]; then
#exit if $2 is no valid url
if ! is_url "$2"; then
usage
exit 1
fi
URL="$2"
fi
MSG="Running containers: $CNT
Expected containers: $NUM"
echo "$MSG"
#report to healthchecks
if [ -n "$URL" ]; then
if [ $NUM -eq $CNT ]; then
curl -m 10 --retry 5 --data-raw "$MSG" "$URL"
else
curl -m 10 --retry 5 --data-raw "$MSG" "$URL/fail"
fi
fi
# **** end ****
exit 0