Files
VPS-scripts_Ubuntu-Backup/Nextcloud-Backup.sh
2020-02-13 10:36:12 +01:00

76 lines
2.7 KiB
Bash

###############################################################################
#
# NEXTCLOUD BackupTool
#
# @author: Bram Prieshof
#
###############################################################################
## Automation
##
## Edit contab (nano /etc/crontab)
## Restart crontab (/etc/init.d/cron restart)
##
#################################################################################
##Configuration
RMDAYS=14 #Days until first backup gets removed
DOMC=01 #Day of month for monthly removal of deleted files
REMOTESERVER=<Remote-Server> #Addres of Backup Server
REMOTEUSER=<user> #User for Backup Server
SYSNAME=<systemName> #System Hostname
BACKUPFROM="/etc/nginx/ /etc/php/ /etc/mysql/ /etc/crontab /opt/ /etc/loolwsd/" #Folders and files that will be backed
DBPASS='<MSQLRootPW>' #MySQL Root password (syntax '<MSQLRootPW>')
DBNAME=<DBName> #Database that will be backed up
ILOCATION="/var/www/<domain>" #Nextcloud instance Location
SITENAME=<Instance name> #Name of instance that will be backed up
#######################################################################
## ##
## PLEASE DO NOT MAKE CHANGES UNDER HERE ##
## ##
#######################################################################
##Setting Static Variables
LFTP=/usr/bin/lftp #Path to binary
TMPDIR=$(mktemp -d) #Your archives are here
SENDDIR=$(mktemp -d) #Sends everything in this folder to ftp
TODAY=$(date --iso) #Today's date like YYYY-MM-DD
RMDATE=$(date --iso -d "$RMDAYS days ago") #TODAY minus X days - too old files
DOM=`date '+%d'` #Day of the month
##Run
#Rsyncing instance files
rsync -e 'ssh -p23' -a ${ILOCATION} "$REMOTEUSER"@"$REMOTESERVER":"$SITENAME"
#Dumping DataBase
mysqldump -u root "$DBNAME" --password="$DBPASS" --default-character-set=utf8 > "$SENDDIR"/"$SITENAME".sql #Dumping DB
#Taring Config files and DB
tar --exclude='/opt/collaboraoffice6.0' --exclude='/opt/lool' -zcf /"$SENDDIR"/"$SYSNAME"-Config-DB.tar.gz $BACKUPFROM #Create tar File
#Uploading and remove first config-db backup on SFTP
$LFTP << EOF
open -u ${REMOTEUSER}, sftp://${REMOTESERVER}
mkdir -p ${SYSNAME}/${TODAY}
cd ${SYSNAME}/${TODAY}
mput -E ${SENDDIR}/*
cd ..
rm -rf ${RMDATE}
bye
EOF
#Checking and running monthly backup to SFTP
if [ $DOM == $DOMC ]; then
rsync -e 'ssh -p23' --delete -a ${ILOCATION} "$REMOTEUSER"@"$REMOTESERVER":"$SITENAME"
fi
#Deletes Temp Folder
rm -r "$TMPDIR" && rm -r "$SENDDIR"