diff --git a/Perso/MATCH_BEGIN.sh b/INTech/MATCH_BEGIN.sh similarity index 100% rename from Perso/MATCH_BEGIN.sh rename to INTech/MATCH_BEGIN.sh diff --git a/Perso/auto_config.sh b/INTech/auto_config.sh similarity index 100% rename from Perso/auto_config.sh rename to INTech/auto_config.sh diff --git a/Perso/INTech/gitUpdate.sh b/INTech/gitUpdate.sh similarity index 100% rename from Perso/INTech/gitUpdate.sh rename to INTech/gitUpdate.sh diff --git a/Perso/.gitignore b/Perso/.gitignore new file mode 100644 index 0000000..59c92fa --- /dev/null +++ b/Perso/.gitignore @@ -0,0 +1 @@ +serialOutput/ diff --git a/Perso/package_restore.sh b/Perso/package_restore.sh deleted file mode 100755 index 7fad345..0000000 --- a/Perso/package_restore.sh +++ /dev/null @@ -1,16 +0,0 @@ -#! /bin/bash - -if [ $(id -u) -ne "0" ]; then - echo "Please execute with superuser privileges" - exit -1 -fi - -cp ./sources.list /etc/apt/sources.list -cp ./preferences.d/my_preferences /etc/apt/preferences.d/my_preferences -cp -r ./sources.list.d/* /etc/apt/sources.list.d - -apt-get update -apt-get install dselect -dpkg --set-selections < "manual_package_save" -apt-get dselect-upgrade -apt-get remove dselect \ No newline at end of file diff --git a/Perso/package_save.sh b/Perso/package_save.sh deleted file mode 100755 index 25908b1..0000000 --- a/Perso/package_save.sh +++ /dev/null @@ -1,63 +0,0 @@ -#! /bin/bash - -name=packages_state -location="$HOME/SYS_SAVES/" - -if [ $# -ne 0 ]; then - if [ ! -z "$1" -a "$1" != "here" ]; then - name=$1 - fi - if [ "$2" = here -o "$1" = here ]; then - location="." - elif [ ! -z "$2" ]; then - echo "Second argument can be void or 'here' only" - exit - fi -fi - -cd $location -pathName="$name""_$(date +'%Y-%m-%d @ %Hh%M')" - -mkdir "$pathName" -cd "./$pathName" -mkdir "./preferences.d" - -cp /etc/apt/sources.list . -cp /etc/apt/preferences.d/my_preferences ./preferences.d/ -cp -r --copy-contents /etc/apt/sources.list.d . - -sudo dpkg --get-selections > "manual_package_save" - -a=C4.sh - -echo "#! /bin/bash" > $a -echo "" >> $a -echo "cd .." >> $a -echo "rm -r \"$pathName\"" >> $a - -chmod u+x "$a" - -b="package_restore.sh" - -echo "#! /bin/bash" > $b -echo "" >> $b -echo 'if [ $(id -u) -ne "0" ]; then' >> $b -echo ' echo "Please execute with superuser privileges"' >> $b -echo " exit -1" >> $b -echo fi >> $b -echo "" >> $b -echo cp ./sources.list /etc/apt/sources.list >> $b -echo cp ./preferences.d/my_preferences /etc/apt/preferences.d/my_preferences >> $b -echo 'cp -r ./sources.list.d/* /etc/apt/sources.list.d' >> $b -echo "" >> $b -echo apt-get update >> $b -echo apt-get install dselect >> $b -echo "dpkg --set-selections < "manual_package_save"" >> $b -echo apt-get dselect-upgrade >> $b -echo apt-get remove dselect >> $b - -chmod u-x "$b" - -cd - - -exit \ No newline at end of file diff --git a/Perso/serialOutput/.gitignore b/Perso/serialOutput/.gitignore deleted file mode 100644 index 5c87cb1..0000000 --- a/Perso/serialOutput/.gitignore +++ /dev/null @@ -1 +0,0 @@ -serialOutput* diff --git a/README.md b/README.md new file mode 100644 index 0000000..ff65606 --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +# Shell scripts + +This repository contains shell scripts that I wrote for myself, my robotics club or my server, mainly in Bash. +It also contains some timer services for systemd. + +## Structure + + - Perso : Personal scripts + - INTech : Scripts made for INTech, my robotics club + - VPS : Scripts mainly used on my server + diff --git a/Perso/AptUpdateWatcher.sh b/VPS/AptUpdateWatcher.sh similarity index 88% rename from Perso/AptUpdateWatcher.sh rename to VPS/AptUpdateWatcher.sh index ee0afbe..c338021 100755 --- a/Perso/AptUpdateWatcher.sh +++ b/VPS/AptUpdateWatcher.sh @@ -1,4 +1,4 @@ -#! /bin/bash +#!/bin/bash # This script updates the apt database if running as root and retrieves the number of upgradable packages. # If the count is high enough or if some important packages can be upgraded, inform the administrator. @@ -30,7 +30,7 @@ if [ -z "$trigger_count" ]; then fi if [ -z "$key_packages" ]; then - key_packages=( "ssh" "ssl" "apache" "kernel" ) + key_packages=( "ssh" "ssl" "apache" "kernel" "linux-image" "syncthing" "wireguard" ) fi @@ -57,5 +57,5 @@ done if [ -n "$output" ]; then output="The Apt Update Watcher has been triggered.\n\n""$output" - echo -e "$output" # Can be replaced by anything or piped into sendmail + echo -e "$output" | mail -aFrom:"$mail_sender" -s "[$(uname -n)] APT update warning $(date +%D-%Hh%M)" "$mail_recipients" fi diff --git a/VPS/FireflyUpdate.sh b/VPS/FireflyUpdate.sh new file mode 100755 index 0000000..e39feda --- /dev/null +++ b/VPS/FireflyUpdate.sh @@ -0,0 +1,68 @@ +#!/bin/bash +set -euo pipefail + +# See documentation at https://docs.firefly-iii.org/advanced-installation/upgrade#created-using-composer-create-project + +if [ "$USER" != "www-data" -a "$USER" != "http" ]; then + echo "Please run this script as the user used by the webserver" + exit 1 +fi + +if [ $# -ne 2 ]; then + echo "Usage : FireflyUpdate /path/to/firefly/rootdir newVersion" + exit 1 +fi + +if [ ! -d "$1" ]; then + echo "Directory does not exist" + exit 1 +fi + + +( + +cd "$1"/.. + +rootDir=$(pwd) +originalDir=$(basename "$1") +newDir="$originalDir"-updated + +echo "Downloading new version..." + +composer create-project grumpydictator/firefly-iii --no-dev --prefer-dist "$originalDir"-updated $2 + +echo "Copying stored data..." + +cp "$1"/.env "$rootDir/$newDir"/.env +cp -r "$1"/storage/{upload,export} "$rootDir/$newDir"/storage +cp "$1"/storage/database/database.sqlite "$rootDir/$newDir"/storage/database/ + +echo "Cleaning caches and running migrations..." + +cd "$newDir" +rm -rf bootstrap/cache/* +php artisan cache:clear +php artisan migrate --seed +php artisan firefly-iii:upgrade-database +php artisan passport:install +php artisan cache:clear + +echo "Forcing access rights to 775 on storage..." + +chmod -R 775 storage + +cd "$rootDir" + +echo "Switching installs..." + +mv "$originalDir" "$originalDir"-old +mv "$newDir" "$originalDir" + +echo "Upgrade done !" +echo "Please check if it was sucessful, if so you can safely run the following command to remove the previous version:" +echo "sudo rm -rf \"$rootDir/$originalDir-old\"" + +) + +exit 0 + diff --git a/VPS/package_save.sh b/VPS/package_save.sh new file mode 100755 index 0000000..2841e31 --- /dev/null +++ b/VPS/package_save.sh @@ -0,0 +1,72 @@ +#! /bin/bash + +name=packages_state +location="$HOME/SYS_SAVES/" + +if [ $# -ne 0 ]; then + if [ ! -z "$1" -a "$1" != "here" ]; then + name=$1 + fi + if [ "$2" = here -o "$1" = here ]; then + location="." + elif [ ! -z "$2" ]; then + echo "Second argument can only be null or 'here'" + exit + fi +fi + +cd $location +pathName="$name""_$(date +'%Y-%m-%d @ %Hh%M')" + +mkdir "$pathName" +cd "$pathName" +mkdir "./preferences.d" + +cp /etc/apt/sources.list . +cp /etc/apt/preferences.d/my_preferences ./preferences.d/ +cp -r --copy-contents /etc/apt/sources.list.d . + +dpkg --get-selections > "manual_package_save" +apt-key exportall > "apt_keys" + +a=C4.sh + +cat < "$a" +#! /bin/bash + +cd .. +rm -r "$pathName" + +EOF + +chmod u+x "$a" + +b="package_restore.sh" + +cat < "$b" +#! /bin/bash" + +if [ $(id -u) -ne "0" ]; then + echo "Please execute with superuser privileges" + exit - +fi + +cp ./sources.list /etc/apt/sources.list +cp ./preferences.d/my_preferences /etc/apt/preferences.d/my_preferences +cp -r ./sources.list.d/* /etc/apt/sources.list.d' + +apt-key add < "apt_keys" +apt-get update +apt-get install dselect +dselect update +dpkg --set-selections < "manual_package_save" +apt-get dselect-upgrade -y +apt-get remove dselect + +EOF + +chmod u-x "$b" + +cd - + +exit diff --git a/VPS/restic-backup.sh b/VPS/restic-backup.sh new file mode 100755 index 0000000..6557dc1 --- /dev/null +++ b/VPS/restic-backup.sh @@ -0,0 +1,92 @@ +#!/bin/bash + +# Use setcap'd binary in home directory +PATH="/home/restic/bin/:$PATH" + +backup_origin="/backups/restic" +backup_container="central-backup" + +if [[ -z "$1" || "$1" != "daily" && "$1" != "weekly" && "$1" != "monthly" ]]; then + echo "Usage : backup [daily|weekly|monthly]" + exit 1 +fi + +include_files="" + +# Use case fallthrough to include each subset of files +# Set the number of backups to keep at the same time using default values +case "$1" in + "monthly") + include_files="$include_files"" --files-from targets/monthly" + backup_count=${backup_count:-6} + ;& + "weekly") + include_files="$include_files"" --files-from targets/weekly" + backup_count=${backup_count:-8} + ;& + "daily") + include_files="$include_files"" --files-from targets/daily" + backup_count=${backup_count:-14} + ;; +esac + +cd "$backup_origin" + +# Run database backups + +echo "Backing up BookStack db..." +mysqldump -u restic BookStack > temp/BookStack_db.mysql +echo "Backing up BitWarden db..." +sqlite3 /home/bitwarden/data/db.sqlite3 ".backup $backup_origin/temp/BitWarden_db.sqlite" +echo "Backing up Firefly db..." +sqlite3 /var/www/firefly-iii/storage/database/database.sqlite ".backup $backup_origin/temp/Firefly_db.sqlite" + +# Backup apt package list and keys +( +cd temp + +echo "Backing up apt keys,sources and packages..." +../package_save.sh here +) + +# Use OVH credentials and backup + +source openstack_creds.sh + +export RESTIC_PASSWORD_FILE="$backup_container".pass +export RESTIC_REPOSITORY="swift:$backup_container:/restic" + +restic backup $include_files --verbose --tag "$1" > backup_output.log 2>&1 +if [ $? -ne 0 ]; then + error_message="Restic backup failed ! See log bellow.\n" + error_message="$error_message""Backup check and purge will not be run.\n\n" + error_message="$error_message""$(cat backup_output.log)" +fi + +if [ -z "$error_message" ]; then + restic check --verbose > check_output.log 2>&1 +fi +if [ $? -ne 0 ]; then + error_message="Restic check failed ! See log below.\n" + error_message="$error_message""Backup purge will not be run.\n\n" + error_message="$error_message""$(cat check_output.log)" +fi + +# Clean up and remove old backups +rm -rf temp/* + +if [ -z "$error_message" ]; then + restic forget --verbose --prune --tag "$1" --keep-last "$backup_count" > forget_output.log 2>&1 +fi +if [ $? -ne 0 ]; then + error_message="Restic forget failed ! See log bellow.\n\n" + error_message="$error_message""$(cat forget_output.log)" +fi + + +if [ -n "$error_message" ]; then + echo -e "$error_message" | mail -aFrom:"$mail_sender" -s "[$(uname -n)] ⚠️ Backup error $(date +%D-%Hh%M)" "$mail_recipients" + exit 1 +fi + +exit 0