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