From ec9dd8e6371f1597797880da811822a93a1b0fc2 Mon Sep 17 00:00:00 2001 From: Daniel Legt Date: Thu, 18 Jan 2024 13:35:51 +0100 Subject: [PATCH] Initial Commit --- .gitignore | 3 ++ bitwarden/README.md | 0 bitwarden/bitwarden_backup.sh | 70 +++++++++++++++++++++++++++++++++++ bitwarden/config.example.sh | 26 +++++++++++++ 4 files changed, 99 insertions(+) create mode 100644 .gitignore create mode 100644 bitwarden/README.md create mode 100755 bitwarden/bitwarden_backup.sh create mode 100644 bitwarden/config.example.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..810b3a7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +**/config.sh +**/backups/* +**/bitwarden_backup_password_decrypt.sh \ No newline at end of file diff --git a/bitwarden/README.md b/bitwarden/README.md new file mode 100644 index 0000000..e69de29 diff --git a/bitwarden/bitwarden_backup.sh b/bitwarden/bitwarden_backup.sh new file mode 100755 index 0000000..bbbea38 --- /dev/null +++ b/bitwarden/bitwarden_backup.sh @@ -0,0 +1,70 @@ +#!/bin/bash + +# Preloader +# Determine the directory of the script +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" + +### [ Variables ] ### +# Import configuration +source "$SCRIPT_DIR/config.sh" + +# Colors +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[0;33m' +NC='\033[0m' # No Color + +### [ Functions ] ### +send_discord_notification() { + local message=$1 + local color=$2 + + # Check if we should send discord notifications + if [ -n "$DISCORD_WEB_HOOK" ]; then + curl \ + -H "Content-Type: application/json" \ + -d "{ \"content\":\"\", \"embeds\":[{ \"title\":\"Vaultwarden Backup\", \"description\":\"${message}\", \"color\":${color}} ]}" \ + $DISCORD_WEB_HOOK + fi +} + +get_7zip_password() { + /home/danlegt/services/backup_scripts/bitwarden/bitwarden_backup_password_decrypt.sh +} + +backup_sqlite_database() { + echo -e "${YELLOW}Backing up SQLite database...${NC}" + sqlite3 $SQLITE_DB_PATH ".backup '$SQLITE_BACKUP_PATH'" + if [ $? -ne 0 ]; then + echo -e "${RED}SQLite database backup failed${NC}" + send_discord_notification "SQLite database backup failed" "16711680" # Red color + exit 1 + fi +} + +compress_and_encrypt_backup() { + echo -e "${YELLOW}Compressing and encrypting backup...${NC}" + 7zz a -p"$(get_7zip_password)" -mhe=on -t7z $BACKUP_FILE $VAULTWARDEN_DATA_DIR $SQLITE_BACKUP_PATH + if [ $? -eq 0 ]; then + echo -e "${GREEN}Backup successful: ${BACKUP_FILE}${NC}" + send_discord_notification "Backup successful: ${BACKUP_FILE}" "65280" # Green color + else + echo -e "${RED}Backup failed${NC}" + send_discord_notification "Backup failed" "16711680" # Red color + exit 1 + fi +} + +delete_old_backups() { + echo -e "${YELLOW}Deleting backups older than ${BACKUP_RETENTION_DAYS} days...${NC}" + find $BACKUP_DIR -type f -name "vaultwarden-backup-*.7z" -mtime +$BACKUP_RETENTION_DAYS -exec rm {} \; + echo -e "${GREEN}Old backups deleted${NC}" +} + +### [ Main ] ### +echo -e "${YELLOW}Starting Vaultwarden backup...${NC}" +send_discord_notification "Starting Vaultwarden backup..." "16776960" # Yellow color + +backup_sqlite_database +compress_and_encrypt_backup +delete_old_backups \ No newline at end of file diff --git a/bitwarden/config.example.sh b/bitwarden/config.example.sh new file mode 100644 index 0000000..69e19d8 --- /dev/null +++ b/bitwarden/config.example.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +########################## +# [ Base Configuration ] # +########################## + +# The target directory to write the .7z files to +BACKUP_DIR="/path/to/backups" +# A discord web hook to send administrative messages through | Leave empty to not send webhook messages +DISCORD_WEB_HOOK="" +# THe vaultwarden data directory +VAULTWARDEN_DATA_DIR="/path/to/bitwarden/data" +# The amount of days to keep data retention locally for +BACKUP_RETENTION_DAYS=30 + +######################### +# [ Generative Config ] # +######################### +# These shouldn't be generally changed + +# The path to the sqlite3 database to properly backup +SQLITE_DB_PATH="$VAULTWARDEN_DATA_DIR/db.sqlite3" +# The path to write the sqlite backup to +SQLITE_BACKUP_PATH="$BACKUP_DIR/database_backup" +# The backup file's location, this is automatically generated but feel free to change +BACKUP_FILE="$BACKUP_DIR/vaultwarden_backup_$(date '+%Y%m%d_%H%M%S').7z" \ No newline at end of file