Created
February 2, 2017 13:17
-
-
Save schiegl/80237da27dc9dbe3f1153ab30677383a to your computer and use it in GitHub Desktop.
Borg Backup Helper Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Backup frontend for borg | |
# | |
# Usage: ./backup.sh <repository> [--backup|--restore <path>|--unmount <path>] | |
# | |
# Dependencies: borg, fuse | |
################################################################################ | |
# # | |
# Backup Configuration # | |
# # | |
################################################################################ | |
# Default backup restore directory? | |
RESTORE_DIR=/tmp/backup_restore | |
# How to name the snapshot? | |
SNAPSHOT_NAME="all-{now:%Y-%m-%d}" | |
# Which backup compression should be applied? | |
COMPRESSION="lz4" | |
# How many snapshots should be kept, after cleaning? | |
ALL_FROM_LAST="10d" # all snaphots from last 10 days | |
WEEKLY=2 # 2 per week | |
MONTHLY=2 # 2 per month | |
YEARLY=2 # 2 per year | |
# Which file manager if not already set? | |
if [ "$FILE_MANAGER" == "" ]; then | |
FILE_MANAGER=dolphin | |
fi | |
# Which files or directories should be saved? | |
declare -a LOCATIONS=( | |
# /home/user/important | |
) | |
# Which directories/regex should be excluded | |
declare -a EXCLUDE=( | |
# 're:^/home/[^/]+/\.thumbnails/' | |
# /home/useless | |
) | |
################################################################################ | |
# # | |
# Program # | |
# # | |
################################################################################ | |
usage() { | |
echo "" | |
echo "Usage:" | |
echo " ./backup.sh <repository-path> [--backup|--restore <path>|--unmount <path>]" | |
echo "" | |
} | |
main() { | |
REPO="$1" | |
if ! [ -d "$REPO" ]; then | |
echo "Directory not found: \"$REPO\"" | |
usage | |
else | |
create_repo_if_not_present $REPO | |
case $2 in | |
--restore ) | |
header "Restore Backup" | |
if [ -d "$3" ]; then | |
RESTORE_DIR="$3" | |
fi | |
restore | |
echo "Done.." | |
break ;; | |
--unmount ) | |
if [ -d "$3" ]; then | |
RESTORE_DIR="$3" | |
fi | |
header "Unmount Backup" | |
echo "Unmounting restore directory at: \"$RESTORE_DIR\"" | |
fusermount -u $RESTORE_DIR | |
echo "Done.." | |
break ;; | |
--backup ) | |
header "Backup" | |
backup | |
break ;; | |
* ) | |
usage | |
break ;; | |
esac | |
fi | |
} | |
backup() { | |
read -sp "Enter passphrase for \"$REPO\": " BORG_PASSPHRASE | |
echo "" | |
export BORG_PASSPHRASE | |
echo "" | |
header "Checking backup consistency" | |
errors=$(borg check $REPO 2>&1) | |
if [[ "${errors,,}" == *"error"* ]] | |
then | |
echo -e "$errors\n" | |
borg check $REPO --repair | |
echo "Done" | |
else | |
echo "Done" | |
header "Creating a new snapshot" | |
borg create -v \ | |
--progress \ | |
--filter=AME \ | |
--compression $COMPRESSION \ | |
--exclude "${EXCLUDE[@]}" \ | |
$REPO::$SNAPSHOT_NAME "${LOCATIONS[@]}" | |
header "Pruning archives" | |
borg prune -v \ | |
--list \ | |
--keep-within=$ALL_FROM_LAST \ | |
--keep-weekly=$WEEKLY \ | |
--keep-monthly=$MONTHLY \ | |
--keep-yearly=$YEARLY \ | |
$REPO | |
echo "Done" | |
fi | |
unset BORG_PASSPHRASE | |
} | |
restore() { | |
read -sp "Enter passphrase for \"$REPO\": " BORG_PASSPHRASE | |
echo "" | |
export BORG_PASSPHRASE | |
IFS=$'\n' | |
snaps=($(borg list $REPO)) | |
unset IFS | |
echo "" | |
echo "Which backup?" | |
echo "" | |
select snapshot in "${snaps[@]}" | |
do | |
if [ -n "$snapshot" ]; then break | |
else | |
echo "Pick a number" | |
fi | |
done | |
# Extract repo name | |
snapshot=$(sed -e 's/\([^\s]\)\s\{2,\}.*/\1/' <<< "$snapshot") | |
mkdir -p $RESTORE_DIR | |
echo "Mounting backup \"$snapshot\" at \"$RESTORE_DIR\"..." | |
borg mount $REPO::$snapshot $RESTORE_DIR | |
unset BORG_PASSPHRASE | |
$FILE_MANAGER $RESTORE_DIR& | |
echo "Don't forget to unmount the restore directory" | |
} | |
create_repo_if_not_present() { | |
REPO="$1" | |
# Naive... | |
if ! [ -d "$REPO/data" ] | |
then | |
notify-send "Backup" "Repository not found" -i "folder-sync" | |
echo "Couldn't find backup repository \"$REPO\", create one there?" | |
select yn in "Yes" "No" | |
do | |
case $yn in | |
Yes ) echo "Creating a backup repository at \"$REPO\"" | |
borg init $REPO | |
break;; | |
No ) exit; break ;; | |
* ) echo "Pick one of the numbers above!" | |
esac | |
done | |
fi | |
} | |
header() { | |
echo "" | |
echo -e "\e[1m$1\e[0m" | |
chars=${#1} | |
for ((i=0;i<chars;i++)); do | |
printf "*" | |
done | |
echo "" | |
echo "" | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment