Merge pull request 'borgbackup script' (#6) from borgbackup into master

Reviewed-on: #6
This commit is contained in:
Lutchy Horace 2024-01-08 14:37:29 -05:00
commit 69b8b2bbfc
1 changed files with 89 additions and 0 deletions

89
borgbackup.sh Executable file
View File

@ -0,0 +1,89 @@
#!/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
# Define the expect script in a variable
expect_script=$(cat <<END_EXPECT
#!/usr/bin/expect -f
# Set the timeout for waiting
set timeout 30
# 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
END_EXPECT
)
eval $PREHOOK
# Save the expect script to a temporary file
expect_script_file=$(mktemp)
echo "$expect_script" > "$expect_script_file"
# Make the temporary expect script executable
chmod +x "$expect_script_file"
# Execute the expect script
$expect_script_file
# Clean up the temporary expect script
rm -f "$expect_script_file"
eval $POSTHOOK