foxly-it / Backup-CloudPanel-Nextcloud

Backup with BorgBackup - CloudPanel, Nextcloud
GNU Lesser General Public License v2.1
5 stars 0 forks source link

Optimized version #3

Open uandhgroup opened 5 days ago

uandhgroup commented 5 days ago
#!/usr/bin/env bash

# Start timer
START=$(date +%s)

# System Information
OS_DISTRO="$(lsb_release -ds)"
OS_ARCH="$(uname -m)"
NUM_CORES=$(nproc || echo 1)

################################################################
# Title          : BBCPv2 - BorgBackup CloudPanel v2           #
# Description    : BorgBackup Nextcloud for Debian/Ubuntu      #
# Author         : Mark Schenk <info@foxly.de>                 #
# Date           : 2024-08-09 14:08                            #
# License        : MIT                                         #
# Version        : 2.0.1                                       #
# Usage          : bash ./backupcpv2.sh                        #
################################################################

#######################
# Configuration       #
#######################

phpversion="8.3"                # PHP version
domain="cloud.example.tld"      # Domain
backupPassword="P@ssw0rd"       # Borg passphrase
backupRepo="/mnt/backup"        # Borg repository path
backupDatabase="/mnt/backup/database" # Database backup path
siteUser="User"                 # Site user
dirBackup="/home/"              # Directory to back up

####################
# Helper Functions  #
####################

backup_VER="v2.0.1"

str_repeat() {
  printf -v v "%-*s" "$1" ""
  echo "${v// /$2}"
}

displaytime() {
  local T=$1
  local D=$((T / 86400))
  local H=$((T / 3600 % 24))
  local M=$((T / 60 % 60))
  local S=$((T % 60))

  printf '%d days %d hours %d minutes and %d seconds\n' $D $H $M $S
}

######################
# Color Definitions   #
######################

FSI='\033['
FRED="${FSI}1;31m"
FGREEN="${FSI}1;32m"
FYELLOW="${FSI}1;33m"
FBLUE="${FSI}1;36m"
FEND="${FSI}0m"

######################
# Check Requirements   #
######################

if [ "$(id -u)" != "0" ]; then
  echo "Error: You must be root or use sudo to run this script"
  exit 1
fi

command_exists() {
  command -v "$@" > /dev/null 2>&1
}

if ! command_exists apt-get; then
  echo "This script can only run on Debian or Ubuntu."
  exit 1
fi

if ! command_exists lsb_release; then
  apt-get update && apt-get install -y lsb-release > /dev/null 2>&1
fi

clear

WELCOME_TXT="Welcome to BBCPv2 - BorgBackup CloudPanel v2 ${backup_VER}"
WELCOME_FEN=${#WELCOME_TXT}

echo -e "\n$(str_repeat "$WELCOME_FEN" "#")\n$WELCOME_TXT\n$(str_repeat "$WELCOME_FEN" "#")\n"
echo "Detected OS     : $OS_DISTRO"
echo "Detected Arch   : $OS_ARCH"
echo "Detected Cores  : $NUM_CORES\n"

#######################
# Backup Process      #
#######################

ncLocation="/home/$siteUser/htdocs/"

echo "Starting Nextcloud Maintenance..."
sudo -u $siteUser php$phpversion "$ncLocation$domain/occ" maintenance:mode --on

echo -e "\e[93mStart MySQL database backup"
bash clpctl db:export --databaseName=DATABASENAME --file="$backupDatabase/backup_$(date +%d-%m-%Y).sql.gz"

export BORG_REPO="$backupRepo"
export BORG_PASSPHRASE="$backupPassword"

info() { printf "\n%s %s\n\n" "$(date)" "$*" >&2; }
trap 'echo "$(date) Backup interrupted" >&2; exit 2' INT TERM

info "Start backup"

borg create --stats --compression lz4 ::'{hostname}-{now}' "$dirBackup"
backup_exit=$?

echo "Ending Nextcloud Maintenance..."
sudo -u $siteUser php$phpversion "$ncLocation$domain/occ" maintenance:mode --off

echo "Backup completed. Storage space usage:"
df -h "$backupRepo"

info "Deleting old backups..."
borg prune --glob-archives '{hostname}-' --keep-daily=7 --keep-weekly=4 --keep-monthly=6
prune_exit=$?

global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))

case $global_exit in
    0) echo -e "${FGREEN}Backup and/or Prune completed successfully after $(displaytime $(($(date +%s) - START)))!${FEND}" ;;
    1) echo -e "${FYELLOW}Backup and/or Prune completed with warnings after $(displaytime $(($(date +%s) - START)))!${FEND}" ;;
    *) echo -e "${FRED}Backup and/or Prune completed with errors after $(displaytime $(($(date +%s) - START)))!${FEND}" ;;
esac

exit $global_exit
foxly-it commented 2 days ago

Hello, thanks for this. Could you make a pull request?