You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.4 KiB

#!/bin/bash
# Author: Tealk
# Repo: https://codeberg.org/Tealk/shell-administration-script
# shellcheck disable=SC2034
if [ ! -d "$BACKPATH" ]; then
mkdir "$BACKPATH"
fi
#Check if storage is mounted
if [ -n "$MOUNT" ]; then
if ! mount | grep "$BACKPATH" >/dev/null; then
mount -t nfs "$MOUNT" "$BACKPATH"
fi
fi
if [ ! -d "$BACKPATH"/mysql ]; then
mkdir "$BACKPATH"/mysql
fi
if [ ! -d "$BACKPATH"/files ]; then
borg init --encryption=none "$BACKPATH"/files
fi
NEXTCLOUDPATH=""
PHPMYADMINPATH=""
MEDIAWIKIPATH=""
if [ "$NCBACKUP" = "true" ]; then
NEXTCLOUD
NEXTCLOUDPATH=$FULLPATH
fi
if [ "$PMABACKUP" = "true" ]; then
PHPMYADMIN
PHPMYADMINPATH=$FULLPATH
fi
if [ "$MWBACKUP" = "true" ]; then
MEDIAWIKI
MEDIAWIKIPATH=$FULLPATH
fi
# Write output to logfile
BLOG="$BACKPATH"/backup-"$DATE".log
exec > >(tee -i "$BLOG")
exec 2>&1
echo "###### Start backup on $(date) ######"
echo "### Creating software.list ..."
# Create list of installed software
dpkg --get-selections >"$BACKPATH"/software.list
# Create database dumps
echo "### Creating database dumps ..."
#read tables
databases=$(mysql -u "$DBBACKUSER" -p"$DBBACKPASSWORD" -e "SHOW DATABASES;" -Nsr | grep -Ev "(information_schema|performance_schema|mysql)")
#backup each table
for db in $databases; do
mysqldump -u "$DBBACKUSER" -p"$DBBACKPASSWORD" "$db" | gzip >"$BACKPATH/mysql/$db-$DATE.sql.gz"
done
# Sync backup data
echo "### Syncing backup files ..."
# shellcheck disable=SC2086
borg create --stats $EXCLUDEPATHS $BACKPATH/files::'{hostname}-{now:%d%m%Y}' $NEXTCLOUDPATH $PHPMYADMINPATH $MEDIAWIKIPATH $BACKUPPATHS
echo "###### Finished backup on $(date) and start pruning ######"
# Pruning file backup
echo "### Pruning file backup"
# shellcheck disable=SC2086
borg prune --stats \
--keep-daily=7 \
--keep-weekly=4 \
--keep-monthly=6 \
$BACKPATH/files
# Pruning mysql backup
echo "### Pruning mysql backup"
OLDMYSQL=$(find "$BACKPATH"/mysql -name '*.sql.gz' | wc -l)
find "$BACKPATH"/mysql -name '*.sql.gz' -mtime +14 -exec rm {} \;
NEWMYSQL=$(find "$BACKPATH"/mysql -name '*.sql.gz' | wc -l)
echo $((OLDMYSQL - NEWMYSQL)) files have been deleted
if [ -n "$LOGCLEANUP" ]; then
echo "### Clean logs"
OLDLOG=$(find "$BACKPATH" -name 'backup-*.log' | wc -l)
find "$BACKPATH" -name 'backup-*.log' -mtime +"$LOGCLEANUP" -exec rm {} \;
NEWLOG=$(find "$BACKPATH" -name 'backup-*.log' | wc -l)
echo $((OLDLOG - NEWLOG)) files have been deleted
fi
echo "###### Pruning finished on $(date) ######"