Last active
May 20, 2019 14:51
-
-
Save flaudisio/d8f3133c158b348aa26b3f5d59fff151 to your computer and use it in GitHub Desktop.
wrestic.sh - minimal wrapper to restic backups
This file contains 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
# Backups | |
30 09-17 * * * root nice -n 12 /usr/local/bin/wrestic.sh backup >> /var/log/wrestic.log 2>&1 | |
40 17 * * * root nice -n 12 /usr/local/bin/wrestic.sh retention-policy >> /var/log/wrestic.log 2>&1 |
This file contains 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
*.iso | |
*.pyc | |
/home/*/.cache | |
/home/*/.config/chromium | |
/home/*/.config/google-chrome | |
/home/*/.config/Franz | |
/home/*/.config/Slack | |
/home/*/.config/spotify | |
/home/*/.dropbox | |
/home/*/.dropbox-dist | |
/home/*/.minikube | |
/home/*/.nvm | |
/home/*/.rbenv | |
/home/*/.steam* | |
/home/*/.terraform.d | |
/home/*/.vagrant.d | |
/home/*/.virtualenvs | |
/home/*/.vscode | |
/home/*/Dropbox | |
/home/*/VirtualBox VMs | |
/root/.cache | |
/root/.dropbox | |
/root/.dropbox-dist | |
/root/.minikube | |
/root/.nvm | |
/root/.terraform.d | |
/root/.vagrant.d | |
/root/.virtualenvs |
This file contains 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
# Hostname | |
Hostname="my-desktop" | |
# Restic | |
# RESTIC_REPOSITORY="s3:https://s3.wasabisys.com/restic-backups/$Hostname" | |
# RESTIC_REPOSITORY="rclone:mega:restic-backups/$Hostname" | |
# RESTIC_REPOSITORY="s3:https://s3.wasabisys.com/restic-backups/$Hostname" | |
RESTIC_REPOSITORY="b2:my-restic-backups:$Hostname" | |
RESTIC_PASSWORD='my_super_secret_password' | |
# Wasabi/S3 | |
# AWS_ACCESS_KEY_ID='MYACCESSKEY' | |
# AWS_SECRET_ACCESS_KEY='MYSECRETKEY' | |
# B2 | |
B2_ACCOUNT_ID='B2ACCOUNTID' | |
B2_ACCOUNT_KEY='B2ACCOUNTKEY' | |
# Backup options | |
ExcludeFile="/etc/wrestic-excludes.txt" | |
# Retention policy | |
KeepLast="3" | |
KeepWithin="5d" | |
KeepDaily="7" | |
KeepWeekly="4" | |
KeepMonthly="12" | |
KeepYearly="3" | |
# Backup dirs | |
BackupDirs=( | |
/etc | |
/home | |
/root | |
/var/www | |
) | |
# Exports | |
export RESTIC_REPOSITORY | |
export RESTIC_PASSWORD | |
# export AWS_ACCESS_KEY_ID | |
# export AWS_SECRET_ACCESS_KEY | |
export B2_ACCOUNT_ID | |
export B2_ACCOUNT_KEY |
This file contains 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
#!/usr/bin/env bash | |
# | |
# wrestic.sh | |
# | |
## | |
[[ -n "$DRY_RUN" ]] && ForgetOpts="--dry-run" | |
ConfigFile="/etc/wrestic.env" | |
end_script() | |
{ | |
echo "==> Finalizing wrestic at $( date --iso-8601=seconds )" | |
echo "--------------------------------------------------------------------" | |
} | |
notify_error() | |
{ | |
local message="$*" | |
if ! command -v notify-send > /dev/null ; then | |
echo "Warning: command 'notify-send' not found" >&2 | |
return 1 | |
fi | |
notify-send --urgency=critical --icon dialog-error "Wrestic Backups" "$message" 2>> /tmp/errors.txt | |
} | |
check_sanity() | |
{ | |
echo "==> Checking environment" | |
if ! command -v restic 2> /dev/null ; then | |
echo "Fatal: command 'restic' not found" >&2 | |
exit 3 | |
fi | |
echo "==> Environment is OK" | |
} | |
load_config() | |
{ | |
echo "==> Loading config" | |
if ! . "$ConfigFile" ; then | |
echo "Fatal: error loading configuration file '$ConfigFile'" >&2 | |
exit 1 | |
fi | |
echo "==> Config file successfully loaded" | |
} | |
run_backup() | |
{ | |
echo "==> Running backup" | |
( | |
set -x | |
restic backup \ | |
--verbose \ | |
--host "$Hostname" \ | |
--exclude-caches \ | |
--exclude-file "$ExcludeFile" \ | |
"${BackupDirs[@]}" | |
) | |
[[ $? -ne 0 ]] && notify_error "Error running the 'backup' command. Please check the logs." | |
echo "==> Backup done" | |
} | |
run_forget() | |
{ | |
local prune_opt="" | |
if [[ "$1" == "--prune" ]] ; then | |
prune_opt="--prune" | |
fi | |
echo "==> Running forget" | |
( | |
set -x | |
restic forget $ForgetOpts $prune_opt \ | |
--verbose \ | |
--compact \ | |
--host "$Hostname" \ | |
--group-by host \ | |
--keep-last "$KeepLast" \ | |
--keep-within "$KeepWithin" \ | |
--keep-daily "$KeepDaily" \ | |
--keep-weekly "$KeepWeekly" \ | |
--keep-monthly "$KeepMonthly" \ | |
--keep-yearly "$KeepYearly" | |
) | |
[[ $? -ne 0 ]] && notify_error "Error running the 'forget' command. Please check the logs." | |
echo "==> Forget done" | |
} | |
run_prune() | |
{ | |
echo "==> Running prune" | |
( | |
set -x | |
restic prune --verbose --cleanup-cache | |
) | |
echo "==> Prune done" | |
} | |
run_check() | |
{ | |
echo "==> Running check" | |
( | |
set -x | |
restic check --verbose --cleanup-cache --check-unused | |
) | |
[[ $? -ne 0 ]] && notify_error "Error running the 'check' command. Please check the logs." | |
echo "==> Check done" | |
} | |
main() | |
{ | |
echo "--------------------------------------------------------------------" | |
echo "==> Starting wrestic at $( date --iso-8601=seconds )" | |
trap end_script EXIT | |
check_sanity | |
load_config | |
echo "==> restic version" | |
restic version | |
case $1 in | |
backup|'') | |
run_backup | |
;; | |
forget) | |
run_forget | |
;; | |
prune) | |
run_prune | |
;; | |
check) | |
run_check | |
;; | |
retention-policy) | |
run_forget --prune | |
run_check | |
;; | |
prune-check) | |
run_prune | |
run_check | |
;; | |
full-cycle) | |
run_backup | |
run_forget | |
run_prune | |
run_check | |
;; | |
*) | |
echo "Fatal: invalid option: $1" >&2 | |
exit 2 | |
;; | |
esac | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment