Skip to content

Instantly share code, notes, and snippets.

@chaddupuis
Created December 21, 2022 18:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chaddupuis/93c83395aef6af6da81ae15f3a154451 to your computer and use it in GitHub Desktop.
Save chaddupuis/93c83395aef6af6da81ae15f3a154451 to your computer and use it in GitHub Desktop.
Borg Backup Bash Script to Hetzner Storage Box
#!/bin/bash
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
## Backup websites and related files along with databases with borg to a storage box on hetzner.
## borg variables
## Must use borg >1.1 on deb 9 $ apt -t stretch-backports install borg-backup
export BORG_RSH="ssh -i /your/key"
export BORG_PASSPHRASE="yourpassphrase"
LOG="/var/log/borg/backup.log"
BACKUP_USER="hetzneruser"
REPOSITORY_DIR="repodirname"
REPOSITORY="ssh://${BACKUP_USER}@${BACKUP_USER}.your-storagebox.de:23/./${REPOSITORY_DIR}"
## output to log
exec > >(tee -i ${LOG})
exec 2>&1
echo "#### Backup Started: $(date) ####"
## get our databases
ExcludeDatabases="Database|information_schema|performance_schema|mysql"
databases=`/usr/bin/mysql -u user -e "SHOW DATABASES;" | tr -d "| " | /bin/egrep -v $ExcludeDatabases` BackUpDir="/wheretheyare/backups"
## date variables
TodaysDate=`date +%Y%m%d`
TwoDaysDate=`date +%Y%m%d --date="2 days ago"`
#Setup or Clear Backups Directory
if [ -d "$BackUpDir" ]; then
rm $BackUpDir/*.bz2
fi
if [ ! -d "$BackUpDir" ]; then
mkdir $BackUpDir
fi
#Dump all the Databases into Backup Directory
for db in $databases; do
echo "Dumping database: $db"
/usr/bin/mysqldump --add-drop-table -u user $db | bzip2 -c > $BackUpDir/$TodaysDate.$db.sql.bz2
done
ExcludeDirectories=""
crontab -l > $BackUpDir/ctab.backup
echo "Transfer files ..."
borg create -v --stats \
$REPOSITORY::'{now:%Y-%m-%d_%H:%M}' \
/user \
/var/www \
/etc/apache2
echo "#### Backup ended: $(date) ####"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment