From 33b0fd79ac1554d15bb21b7523be12e3d1decda5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roger=20P=C3=A0mies?= Date: Thu, 14 Jul 2022 13:48:25 +0200 Subject: [PATCH] add scripts --- backup_clean.sh | 21 +++++++++++++++++++++ backup_db.sh | 24 ++++++++++++++++++++++++ backup_dir.sh | 22 ++++++++++++++++++++++ backup_inc.sh | 38 ++++++++++++++++++++++++++++++++++++++ backup_total.sh | 34 ++++++++++++++++++++++++++++++++++ daily_backups.sh | 31 +++++++++++++++++++++++++++++++ weekly_backups.sh | 31 +++++++++++++++++++++++++++++++ 7 files changed, 201 insertions(+) create mode 100644 backup_clean.sh create mode 100644 backup_db.sh create mode 100644 backup_dir.sh create mode 100644 backup_inc.sh create mode 100644 backup_total.sh create mode 100644 daily_backups.sh create mode 100644 weekly_backups.sh diff --git a/backup_clean.sh b/backup_clean.sh new file mode 100644 index 0000000..21aa530 --- /dev/null +++ b/backup_clean.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +PATH=$PATH:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin + +#Use backup_clean.sh + +LOG_FILE=$1 +REMOTE_SERVER=$2 +REMOTE_PATH=$3 +BACKUP_PRESERVE_DAYS=21 + +echo "\n[NEW BACKUP TASK] Clear $BACKUP_PRESERVE_DAYS days old backups\n" >> $LOG_FILE +echo "\nThese are the remaining backups:\n" +# Clear X days old remote logs +ssh $REMOTE_SERVER 'bash -s' << EOF >> $LOG_FILE +find $REMOTE_PATH -maxdepth 1 -mtime +$BACKUP_PRESERVE_DAYS -print -exec "rm" -R {} \; +find $REMOTE_PATH -mindepth 1 -maxdepth 1 -mtime -$BACKUP_PRESERVE_DAYS -print | sort -n +EOF + +echo "\n[DONE BACKUPS] Clear old backups completed!\n" >> $LOG_FILE +echo "########################################################" >> $LOG_FILE diff --git a/backup_db.sh b/backup_db.sh new file mode 100644 index 0000000..2fe3cbb --- /dev/null +++ b/backup_db.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +PATH=$PATH:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin + +#Use backup_db.sh + +LOG_FILE=$1 +BACKUP_FILE="nextcloud-sqlbkp-"`date +\%Y\%m\%d`".sql" +DB_HOST="localhost" +DB_USER="user" +DB_PASS="password" +DB_NAME="dbname" +PATH_LOCAL_BACKUPS=$2 +REMOTE_SERVER=$3 +REMOTE_PATH=$4 + +echo "\n[NEW BACKUP TASK] Backup database nextcloud...\n" >> $LOG_FILE + +# dump and backup db nextcloud handycat and rsync deleting origin +mysqldump --single-transaction --verbose -h $DB_HOST -u $DB_USER -p$DB_PASS $DB_NAME 2>> $LOG_FILE > $PATH_LOCAL_BACKUPS$BACKUP_FILE +rsync -AaxzPh --remove-source-files $PATH_LOCAL_BACKUPS$BACKUP_FILE "$REMOTE_SERVER:$REMOTE_PATH" >> $LOG_FILE + +echo "\n[DONE BACKUPS] handycat db nextcloud backup completed!\n" >> $LOG_FILE +echo "########################################################" >> $LOG_FILE diff --git a/backup_dir.sh b/backup_dir.sh new file mode 100644 index 0000000..6fe8903 --- /dev/null +++ b/backup_dir.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +PATH=$PATH:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin + +#Use backup_dir.sh + +LOG_FILE=$1 +BACKUP_FILE="nextcloud-dirbkp_"`date +\%Y\%m\%d`".tar.gz" +EXCLUDE_DIRS="/path/to/nextcloud/data" +EXCLUDE_DIRS="${EXCLUDE_DIRS} /path/to/nextcloud/" +PATH_LOCAL_BACKUPS=$2 +REMOTE_SERVER=$3 +REMOTE_PATH=$4 + +echo "\n[NEW BACKUP TASK] Backup install dir nextcloud...\n" >> $LOG_FILE + +# tar and backup install dir nextcloud, excluding data folder and finally rsync deleting origin +tar zcvf $PATH_LOCAL_BACKUPS$BACKUP_FILE --exclude=$EXCLUDE_DIRS +rsync -AaxzPh --remove-source-files $PATH_LOCAL_BACKUPS$BACKUP_FILE "$REMOTE_SERVER:$REMOTE_PATH" >> $LOG_FILE + +echo "[DONE BACKUPS] handycat dir nextcloud backup completed!" >> $LOG_FILE +echo "########################################################" >> $LOG_FILE diff --git a/backup_inc.sh b/backup_inc.sh new file mode 100644 index 0000000..fa2cfb5 --- /dev/null +++ b/backup_inc.sh @@ -0,0 +1,38 @@ +#!/bin/bash + +PATH=$PATH:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin + +#Use backup_inc.sh + +LOG_FILE=$1 +BACKUP_DIR="nextcloud-datainc_"`date +\%Y\%m\%d`"/" +EXCLUDE_FILE="file-list.txt" +ORIGIN_DATA_PATH=$2 +REMOTE_SERVER=$3 +REMOTE_PATH=$4 +LATEST_LINK="latest" +CURRENT_DATE=`date +\%y\%m\%d\%H\%M.\%S` + +touch $EXCLUDE_FILE +cat > $EXCLUDE_FILE <> $LOG_FILE + +# rsync of new files last day into nextcloud every day (incremental) +rsync -AaxzvPh --delete $ORIGIN_DATA_PATH --link-dest "../$LATEST_LINK" --exclude-from=$EXCLUDE_FILE "$REMOTE_SERVER:$REMOTE_PATH$BACKUP_DIR" >> $LOG_FILE + +# set current date to sync remote folder +ssh $REMOTE_SERVER "touch -t $CURRENT_DATE $REMOTE_PATH$BACKUP_DIR 2>&1" >> $LOG_FILE + +# replace latest +ssh $REMOTE_SERVER "rm -rf $REMOTE_PATH$LATEST_LINK && ln -s $REMOTE_PATH$BACKUP_DIR $REMOTE_PATH$LATEST_LINK 2>&1" >> $LOG_FILE + +rm $EXCLUDE_FILE + +echo "\n[DONE BACKUPS] handycat incremental backup completed!\n" >> $LOG_FILE +echo "########################################################" >> $LOG_FILE diff --git a/backup_total.sh b/backup_total.sh new file mode 100644 index 0000000..d62c7f1 --- /dev/null +++ b/backup_total.sh @@ -0,0 +1,34 @@ +#!/bin/bash + +PATH=$PATH:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin + +#Use backup_inc.sh + +LOG_FILE=$1 +BACKUP_DIR="nextcloud-datatotal_"`date +\%Y\%m\%d`"/" +EXCLUDE_FILE="file-list.txt" +ORIGIN_DATA_PATH=$2 +REMOTE_SERVER=$3 +REMOTE_PATH=$4 +LATEST_LINK="latest" + +touch $EXCLUDE_FILE +cat > $EXCLUDE_FILE <> $LOG_FILE + +# rsync of new files last day into nextcloud every day (incremental) +rsync -AaxzvPh $ORIGIN_DATA_PATH --exclude-from=$EXCLUDE_FILE "$REMOTE_SERVER:$REMOTE_PATH$BACKUP_DIR" >> $LOG_FILE + +# replace latest +ssh $REMOTE_SERVER "rm -rf $REMOTE_PATH$LATEST_LINK && ln -s $REMOTE_PATH$BACKUP_DIR $REMOTE_PATH$LATEST_LINK 2>&1" >> $LOG_FILE + +rm $EXCLUDE_FILE + +echo "\n[DONE BACKUPS] handycat total backup completed!\n" >> $LOG_FILE +echo "########################################################" >> $LOG_FILE diff --git a/daily_backups.sh b/daily_backups.sh new file mode 100644 index 0000000..2ce6a9a --- /dev/null +++ b/daily_backups.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +PATH=$PATH:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin + +GOTIFY_TOKEN="token" +GOTIFY_MESSAGE="Backup task init: **"`date +"%d/%m/%Y %H:%M:%S"`"** \r" +LOG_FILE="hostname_"`date +"%d-%m-%Y"`".txt" +PATH_LOCAL_BACKUPS="/path/to/temporal/backups/" +PATH_ORIGIN_DATA="/path/to/nextcloud/data/" +REMOTE_SERVER="remote.example.com" +REMOTE_PATH="/path/to/remote/store/backups/" + +mkdir -p $PATH_LOCAL_BACKUPS +touch $LOG_FILE + +sh ~/backup_db.sh "$LOG_FILE" "$PATH_LOCAL_BACKUPS" "$REMOTE_SERVER" "$REMOTE_PATH" +sh ~/backup_dir.sh "$LOG_FILE" "$PATH_LOCAL_BACKUPS" "$REMOTE_SERVER" "$REMOTE_PATH" +sh ~/backup_inc.sh "$LOG_FILE" "$PATH_ORIGIN_DATA" "$REMOTE_SERVER" "$REMOTE_PATH" +sh ~/backup_clean.sh "$LOG_FILE" "$REMOTE_SERVER" "$REMOTE_PATH" + +# send log to logs public archive +LOG_URL=$(sh ~/logger.sh "$LOG_FILE") + +# delete local log +rm $LOG_FILE + +GOTIFY_MESSAGE="${GOTIFY_MESSAGE} Backup task end: **"`date +"%d/%m/%Y %H:%M:%S"`"** \r" +GOTIFY_MESSAGE="${GOTIFY_MESSAGE} [LogFile]($LOG_URL)" + +# send gotify notification +sh ~/gotifypush.sh "NC Inc Backup" "$GOTIFY_MESSAGE" 5 "$GOTIFY_TOKEN" "$LOG_URL" diff --git a/weekly_backups.sh b/weekly_backups.sh new file mode 100644 index 0000000..55c35c6 --- /dev/null +++ b/weekly_backups.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +PATH=$PATH:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin + +GOTIFY_TOKEN="token" +GOTIFY_MESSAGE="Backup task init: **"`date +"%d/%m/%Y %H:%M:%S"`"** \r" +LOG_FILE="hostname_"`date +"%d-%m-%Y"`".txt" +PATH_LOCAL_BACKUPS="/path/to/temporal/backups/" +PATH_ORIGIN_DATA="/path/to/nextcloud/data/" +REMOTE_SERVER="remote.example.com" +REMOTE_PATH="/path/to/remote/store/backups/" + +mkdir -p $PATH_LOCAL_BACKUPS +touch $LOG_FILE + +sh ~/backup_db.sh "$LOG_FILE" "$PATH_LOCAL_BACKUPS" "$REMOTE_SERVER" "$REMOTE_PATH" +sh ~/backup_dir.sh "$LOG_FILE" "$PATH_LOCAL_BACKUPS" "$REMOTE_SERVER" "$REMOTE_PATH" +sh ~/backup_total.sh "$LOG_FILE" "$PATH_ORIGIN_DATA" "$REMOTE_SERVER" "$REMOTE_PATH" +sh ~/backup_clean.sh "$LOG_FILE" "$REMOTE_SERVER" "$REMOTE_PATH" + +# send log to logs public archive +LOG_URL=$(sh ~/logger.sh "$LOG_FILE") + +# delete local log +rm $LOG_FILE + +GOTIFY_MESSAGE="${GOTIFY_MESSAGE} Backup task end: **"`date +"%d/%m/%Y %H:%M:%S"`"** \r" +GOTIFY_MESSAGE="${GOTIFY_MESSAGE} [LogFile]($LOG_URL)" + +# send gotify notification +sh ~/gotifypush.sh "NC Total Backup" "$GOTIFY_MESSAGE" 5 "$GOTIFY_TOKEN" "$LOG_URL"