diff --git a/bitwarden/bitwarden_backup.sh b/bitwarden/bitwarden_backup.sh index 218e4f0..3fc504f 100755 --- a/bitwarden/bitwarden_backup.sh +++ b/bitwarden/bitwarden_backup.sh @@ -4,47 +4,17 @@ # Determine the directory of the script SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" -### [ Variables ] ### +PROGRAM_NAME="Gitea Backup Script" + +### [ Imports ] ### +# Import Colors +source "$SCRIPT_DIR/../lib/colors.sh" # 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 +# Import Functions +source "$SCRIPT_DIR/../lib/functions.sh" ### [ Functions ] ### -check_required_programs() { - local missing=0 - for program in gdrive 7zz sqlite3 curl; do - if ! command -v $program &> /dev/null; then - echo -e "${RED}Error: Required program '$program' is not installed.${NC}" >&2 - missing=1 - fi - done - - if [ $missing -ne 0 ]; then - local ERR_MSG="One or more required programs are missing." - send_discord_notification "$ERR_MSG" "16711680" - echo -e "${RED}$ERR_MSG${NC}" >&2 - exit 1 - fi -} - -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() { $SCRIPT_DIR/bitwarden_backup_password_decrypt.sh } @@ -132,10 +102,10 @@ delete_old_gdrive_backups() { } ### [ Main ] ### -echo -e "${YELLOW}Starting Vaultwarden backup...${NC}" +echo -e "${YELLOW}Starting ${PROGRAM_NAME}...${NC}" send_discord_notification "Starting Vaultwarden backup..." "16776960" # Yellow color -check_required_programs +check_required_programs "$SCRIPT_DIR/required_programs.txt" backup_sqlite_database compress_and_encrypt_backup diff --git a/bitwarden/bitwarden_backup_password_decrypt.example.sh b/bitwarden/bitwarden_backup_password_decrypt.example.sh new file mode 100755 index 0000000..11f165d --- /dev/null +++ b/bitwarden/bitwarden_backup_password_decrypt.example.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +# Here you can/should run a short script of getting your +# password as plain text, I personally recommend +# gpg key encryption of your key and decrypting it +# at runtime, it really depends on what kind of setup you have + +echo -e "example_password"; \ No newline at end of file diff --git a/bitwarden/required_programs.txt b/bitwarden/required_programs.txt new file mode 100644 index 0000000..278cab3 --- /dev/null +++ b/bitwarden/required_programs.txt @@ -0,0 +1,4 @@ +*gdrive +7zz +sqlite3 +*curl \ No newline at end of file diff --git a/gitea/README.md b/gitea/README.md new file mode 100644 index 0000000..e69de29 diff --git a/gitea/config.example.sh b/gitea/config.example.sh new file mode 100644 index 0000000..4ead07c --- /dev/null +++ b/gitea/config.example.sh @@ -0,0 +1,18 @@ + +# The name of the gitea container, this is required. +GITEA_CONTAINER_NAME="gitea_app" +# An absolute path to the gitea app.ini from within the docker container +GITEA_CONF_PATH=/data/gitea/conf/app.ini +# The path where WITHING THE CONTAINER the dump will be placed +GITEA_BACKUP_CONTAINER_LOCATION=/backups + +# The path that was mounted for gitea to push backups to +BACKUP_SOURCE_PATH=/home/x/services/gitea/backups +# Define the number of backups to keep (e.g., keep the latest 7 backups) +KEEP_BACKUP_COUNT=3 + +# Discord Settings +DISCORD_WEBHOOK= + +# Google Drive Settings +GOOGLE_DRIVE_FOLDER_ID= diff --git a/gitea/gitea_backup.sh b/gitea/gitea_backup.sh new file mode 100644 index 0000000..9ec088a --- /dev/null +++ b/gitea/gitea_backup.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +# Preloader +# Determine the directory of the script +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" + +PROGRAM_NAME="Gitea Backup Script" + +### [ Imports ] ### +# Import Colors +source "$SCRIPT_DIR/../lib/colors.sh" +# Import configuration +source "$SCRIPT_DIR/config.sh" +# Import Functions +source "$SCRIPT_DIR/../lib/functions.sh" + +### [ Functions ] ### + +backup_from_docker() { + # Reference to the gitea container id + GITEA_DOCKER_CONTAINER=$(docker ps -qf name="${GITEA_CONTAINER_NAME}") + + # Run the backup process + if docker exec -w $GITEA_BACKUP_CONTAINER_LOCATION -u git $GITEA_DOCKER_CONTAINER bash -c "/usr/local/bin/gitea dump -c $GITEA_CONF_PATH"; then + log "Gitea backup created successfully." + else + error "Failed to create Gitea backup." + send_discord_notification "Failed to create Gitea backup." + exit 1 + fi + + # Determine the path to the output file + BACKUP_FILE=$(ls -Art $BACKUP_SOURCE_PATH/gitea-dump-*.zip | tail -n 1) + + if [ -z "$BACKUP_FILE" ]; then + error "Backup file not found." + send_discord_notification "Backup file not found." + exit 1 + fi + + log "Backup file $BACKUP_FILE created." +} + +### [ Main ] ### +echo -e "${YELLOW}Starting ${PROGRAM_NAME}...${NC}" +send_discord_notification "Starting Vaultwarden backup..." "16776960" # Yellow color + +check_required_programs "$SCRIPT_DIR/required_programs.txt" + +backup_from_docker + +# Run google drive backup + +# Final message +log "Backup process completed." +send_discord_notification "Backup process completed." \ No newline at end of file diff --git a/gitea/gitea_password.sh b/gitea/gitea_password.sh new file mode 100644 index 0000000..11f165d --- /dev/null +++ b/gitea/gitea_password.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +# Here you can/should run a short script of getting your +# password as plain text, I personally recommend +# gpg key encryption of your key and decrypting it +# at runtime, it really depends on what kind of setup you have + +echo -e "example_password"; \ No newline at end of file diff --git a/gitea/required_programs.txt b/gitea/required_programs.txt new file mode 100644 index 0000000..6692088 --- /dev/null +++ b/gitea/required_programs.txt @@ -0,0 +1,3 @@ +docker +*gdrive +*curl \ No newline at end of file diff --git a/lib/colors.sh b/lib/colors.sh new file mode 100755 index 0000000..2ab47db --- /dev/null +++ b/lib/colors.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +# Colors +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[0;33m' +NC='\033[0m' # No Color \ No newline at end of file diff --git a/lib/functions.sh b/lib/functions.sh new file mode 100755 index 0000000..9defae5 --- /dev/null +++ b/lib/functions.sh @@ -0,0 +1,68 @@ +#!/bin/bash + +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 +} + +check_required_programs() { + local requirements_file=$1 + local missing=0 + local optional_missing=0 + + while IFS= read -r line || [[ -n "$line" ]]; do + program=${line#\*} + is_optional=false + + if [[ "$line" == \** ]]; then + is_optional=true + fi + + if ! command -v "$program" &> /dev/null; then + if [ "$is_optional" = true ]; then + echo -e "${YELLOW}Warning: Optional program '$program' is not installed.${NC}" >&2 + optional_missing=1 + else + echo -e "${RED}Error: Required program '$program' is not installed.${NC}" >&2 + missing=1 + fi + fi + done < "$requirements_file" + + if [ $missing -ne 0 ]; then + local ERR_MSG="One or more required programs are missing." + send_discord_notification "$ERR_MSG" "16711680" + echo -e "${RED}$ERR_MSG${NC}" >&2 + exit 1 + elif [ $optional_missing -ne 0 ]; then + local WARN_MSG="One or more optional programs are missing." + send_discord_notification "$WARN_MSG" "16776960" # Yellow color code + echo -e "${YELLOW}$WARN_MSG${NC}" >&2 + fi +} + +# Logging + +success() { + echo -e "${GREEN}Success: $1${NC}" +} + +log() { + echo -e "${NC}$1${NC}" +} + +warn() { + echo -e "${YELLOW}WARN: $1${NC}" +} + +error() { + echo -e "${RED}ERROR: $1${NC}" >&2 +} \ No newline at end of file