scripts/borgbackup.sh

90 lines
1.8 KiB
Bash
Executable File

#!/usr/bin/env bash
#/////////////////////////////////////////////////////
#
#
# BORG BACKUP
#
#
#/////////////////////////////////////////////////////
# NOTE: Make sure the repository is initialize with borg init
# first before creating a backup!
# NOTE: Make sure the expect command is installed on the system!
# NOTE: All these variables can be placed in /etc/borgbackup.conf
# Path to repository
# Example: user@server:/path/on/remote
# NOTE: Make sure it ends with / or face the consquences!
REPOSITORY=''
# Passphrase for borg backup.
# NOTE: If you don't want to include the passphrase here.
# You can include it in /etc/borgbackup.conf
PASSPHRASE=''
# Source directory to backup
# Example: /path/to/backup
SOURCE='/'
# Exclude paths from backup.
# Example: --exclude /exclude1 --exclude /exclude2
EXCLUDES=''
# Run commands before backup
# Example: systemctl stop mysqld
PREHOOK=""
# Run commands after backup
# Example: systemctl start mysqld
POSTHOOK=""
# DO NOT MODIFY ANY CODE BELOW THIS LINE
if test -f /etc/borgbackup.conf; then
source /etc/borgbackup.conf
fi
echo "Running PREHOOK..."
eval $PREHOOK
echo "Running borg backup.."
expect <<END_EXPECT
#!/usr/bin/expect -f
set timeout -1
# Set the passphrase
set _passphrase "$PASSPHRASE"
# Spawn the command
spawn /bin/borg create --stats -v $REPOSITORY::'{hostname}-{now:%Y-%m-%d}' $SOURCE $EXCLUDES
# Expect the passphrase prompt and send the passphrase
expect "Enter passphrase for key*"
send "\$_passphrase\r"
# Wait for the command to finish
expect eof
foreach {pid spawnid os_error_flag value} [wait] break
if {\$os_error_flag == 0} {
puts "exit status: \$value"
exit \$value
} else {
puts "errno: \$value"
exit \$value
}
END_EXPECT
echo "Borg exit status: exit_status=$?"
echo "Running POSTHOOK..."
eval $POSTHOOK
exit $exit_status