borg/backup.sh

106 lines
2.2 KiB
Bash
Executable File

#!/bin/bash
# Create a backup, prune old backups, compact the repository, and list the contents
backup() {
#check PRE_COMMAND exists
if [[ -n $PRE_COMMAND ]]; then
echo "Running pre-command"
eval $PRE_COMMAND
fi
create
prune
compact
list
#check POST_COMMAND exists
if [[ -n $POST_COMMAND ]]; then
echo "Running post-command"
eval $POST_COMMAND
fi
}
# Initialize the repository
init() {
echo "Initializing repository"
/usr/local/bin/borg init --encryption=repokey $BORG_REPO
}
# Create a backup
create() {
echo "Creating backup"
/usr/local/bin/borg create --compression zstd,1 $BORG_REPO::'{hostname}-{now}' /src
}
# List the contents of the repository
list() {
echo "Listing repository"
/usr/local/bin/borg list $BORG_REPO
}
PRUNE=""
# Clean up old backups
prune() {
echo "Pruning repository"
if [[ -n $MINUTELY ]]; then
PRUNE="--keep-minutely ${MINUTELY}"
fi
if [[ -n $HOURLY ]]; then
PRUNE="${PRUNE} --keep-hourly ${HOURLY}"
fi
if [[ -n $DAILY ]]; then
PRUNE="${PRUNE} --keep-daily ${DAILY}"
fi
if [[ -n $WEEKLY ]]; then
PRUNE="${PRUNE} --keep-weekly ${WEEKLY}"
fi
if [[ -n $MONTHLY ]]; then
PRUNE="${PRUNE} --keep-monthly ${MONTHLY}"
fi
/usr/local/bin/borg prune $PRUNE $BORG_REPO
}
# Compact the repository
compact() {
echo "Compacting repository"
/usr/local/bin/borg compact $BORG_REPO
}
delete() {
echo "Deleting repository"
/usr/local/bin/borg delete $BORG_REPO $2
}
restore() {
echo "Restoring backup"
echo $2
#Restore dir
cd $RESTORE_PATH
/usr/local/bin/borg -v extract ${BORG_REPO}::$BACKUP
}
if [[ $1 == "backup" ]]; then
backup
elif [[ $1 == "init" ]]; then
init
elif [[ $1 == "create" ]]; then
create
elif [[ $1 == "list" ]]; then
list
elif [[ $1 == "prune" ]]; then
prune
elif [[ $1 == "compact" ]]; then
compact
elif [[ $1 == "delete" ]]; then
delete
elif [[ $1 == "restore" ]]; then
BACKUP=$2
RESTORE_PATH=$3
restore
elif [[ $1 == "bash" ]]; then
bash
elif [[ $1 == "server" ]]; then
/usr/local/bin/borg serve --restrict-to-path /data
else
echo "Invalid command"
fi