add scripts

This commit is contained in:
Roger Pàmies Fabra 2022-07-14 13:48:25 +02:00
parent 9a13e3dae5
commit 33b0fd79ac
7 changed files with 201 additions and 0 deletions

21
backup_clean.sh Normal file
View File

@ -0,0 +1,21 @@
#!/bin/bash
PATH=$PATH:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
#Use backup_clean.sh <logfile> <remote_server> <remote_path>
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

24
backup_db.sh Normal file
View File

@ -0,0 +1,24 @@
#!/bin/bash
PATH=$PATH:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
#Use backup_db.sh <logfile> <path_local_backups> <remote_server> <remote_path>
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

22
backup_dir.sh Normal file
View File

@ -0,0 +1,22 @@
#!/bin/bash
PATH=$PATH:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
#Use backup_dir.sh <logfile> <path_local_backups> <remote_server> <remote_path>
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

38
backup_inc.sh Normal file
View File

@ -0,0 +1,38 @@
#!/bin/bash
PATH=$PATH:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
#Use backup_inc.sh <logfile> <origin_data_path> <remote_server> <remote_path>
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 <<EOF
files_trashbin
nextcloud.log
updater.log
EOF
echo "\n[NEW BACKUP TASK] Backup cloud new/modified files of last day (incremental)\n" >> $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

34
backup_total.sh Normal file
View File

@ -0,0 +1,34 @@
#!/bin/bash
PATH=$PATH:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
#Use backup_inc.sh <logfile> <origin_data_path> <remote_server> <remote_path>
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 <<EOF
files_trashbin
nextcloud.log
updater.log
EOF
echo "\n[NEW BACKUP TASK] Backup cloud all files (total)\n" >> $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

31
daily_backups.sh Normal file
View File

@ -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"

31
weekly_backups.sh Normal file
View File

@ -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"