Borgbackup
Page content
Prerequisite
- you need a remote Borg Server (Unix/Linux Machine with Borg installed)
- valid User and Key for SCP Transfer
- SSH Key -> /backup/id_ed25519
Create Local Folder
test -d /backup || (mkdir /backup; chmod 700 /backup)
Borg Backup Script
cat << 'EOF2' > /backup/borg.sh
#!/usr/bin/env bash
# BorgBackup Script, v1.0, 2024-04-09, by @stoege
# Remote server details
REMOTE_USER="borguser"
REMOTE_HOST="your.remote.borg.server"
REMOTE_REPO="mysamplerepo"
# Local directory to backup
LOCAL_DIR="/"
# List of directories to exclude
EXCLUDE_DIRS=(
"*/.cache/"
"/tmp"
"/restore"
)
# Set PassPhrase for the Backup Encryption (run: pwgen 32 1)
export BORG_PASSPHRASE="your-super-strong-password"
export BORG_RSH='ssh -i /backup/id_ed25519'
# Function to perform full backup
perform_full_backup() {
# Construct exclude options
exclude_opts=""
for dir in "${EXCLUDE_DIRS[@]}"; do
exclude_opts+="--exclude $dir "
done
# Run BorgBackup command
borg create \
--verbose \
--stats \
--progress \
--compression lz4 \
$REMOTE_USER@$REMOTE_HOST:$REMOTE_REPO::'{hostname}-{now:%Y-%m-%d_%H:%M:%S}' \
$LOCAL_DIR \
$exclude_opts \
|| borg init -e repokey-blake2 $REMOTE_USER@$REMOTE_HOST:$REMOTE_REPO
}
# Function to perform restore
perform_restore() {
# Create Restore
test -d /restore || mkdir /restore
# Change Dir
cd /restore
# Run BorgBackup command to restore specific directory
borg extract \
--verbose \
--progress \
$REMOTE_USER@$REMOTE_HOST:$REMOTE_REPO::$1 \
$2
}
# Function to list all backups
list_backups() {
# Run BorgBackup command to list all archives
borg list $REMOTE_USER@$REMOTE_HOST:$REMOTE_REPO
}
# Function to rotate backups
rotate_backups() {
# Run BorgBackup command to prune archives based on retention policy
borg prune \
--verbose \
--list \
--glob-archives "${hostname}*" \
--keep-hourly=2 \
--keep-daily=2 \
--keep-weekly=2 \
--keep-monthly=2 \
$REMOTE_USER@$REMOTE_HOST:$REMOTE_REPO
}
# Install on Host
install_myself() {
# Folder
f="/backup"
# Create Directory, copy File
test -d ${f} || (mkdir ${f}; chmod 700 ${f})
cp $0 ${f}/
# Inform User
cat << EOF
# to install in Crontab:
crontab -e
5 6,12,18 * * * cd ${f}; $0 backup >> /var/log/borgbackup.log 2>&1
EOF
}
# Help
show_help() {
echo "$0 [ backup | list | rotate | install | restore BACKUPNAME /etc ]"
exit 1
}
# Main script
echo "Starting BorgBackup..."
# Check if BorgBackup is installed
if ! command -v borg &> /dev/null; then
echo "Error: BorgBackup is not installed. Please install BorgBackup."
exit 1
fi
# Check if parameter is provided
if [ $# -eq 0 ]; then
show_help
fi
# Perform action based on parameter
case "$1" in
"backup")
echo "Performing full backup..."
perform_full_backup
rotate_backups
;;
"restore")
if [ $# -lt 3 ]; then
echo "Error: Please specify a Backup Set and directory to restore."
echo "$0 BACKUP_SET /FOLDER/TO/RESTORE"
exit 1
fi
echo "Performing restore for directory '$3' on set '$2'"
perform_restore $2 $3
;;
"list")
echo "Listing all backups..."
list_backups
;;
"rotate")
echo "Rotating backups..."
rotate_backups
;;
"install")
echo "Install Scripts"
install_myself
;;
*)
show_help
;;
esac
# Check backup status
if [ $? -eq 0 ]; then
echo "Action completed successfully."
else
echo "Action failed."
fi
# Finally done
exit 0
EOF2
chmod 700 /backup/borg.sh
Execute It
Create Backup
/backup/borg.sh backup
List Backup
/backup/borg.sh list
Restore Folder
/backup/borg.sh restore hostname-date /etc"
Any Comments ?
sha256: 3adc039f17d2b87ef48b8e9d200c53675b430603c048d4879aacb2dabb3ce37f