1
0
Fork 0

Compare commits

..

5 commits

12 changed files with 247 additions and 83 deletions

1
Perso/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
serialOutput/

View file

@ -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

View file

@ -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

View file

@ -1 +0,0 @@
serialOutput*

11
README.md Normal file
View file

@ -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

View file

@ -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. # 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. # 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 fi
if [ -z "$key_packages" ]; then if [ -z "$key_packages" ]; then
key_packages=( "ssh" "ssl" "apache" "kernel" ) key_packages=( "ssh" "ssl" "apache" "kernel" "linux-image" "syncthing" "wireguard" )
fi fi
@ -57,5 +57,5 @@ done
if [ -n "$output" ]; then if [ -n "$output" ]; then
output="The Apt Update Watcher has been triggered.\n\n""$output" 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 fi

68
VPS/FireflyUpdate.sh Executable file
View file

@ -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

72
VPS/package_save.sh Executable file
View file

@ -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 <<EOF > "$a"
#! /bin/bash
cd ..
rm -r "$pathName"
EOF
chmod u+x "$a"
b="package_restore.sh"
cat <<EOF > "$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

92
VPS/restic-backup.sh Executable file
View file

@ -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