borg.sh 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env bash
  2. set -euo pipefail # bash strict mode
  3. set -x
  4. # Setting this, so the repo does not need to be given on the commandline:
  5. export BORG_REPO="$BACKUPS"
  6. # some helpers and error handling:
  7. info() { printf "\n%s %s\n\n" "$( date )" "$*" >&2; }
  8. trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM
  9. info "Starting backup"
  10. # Backup the most important directories into an archive named after
  11. # the machine this script is currently running on:
  12. script_dir="${0%/*}" # directory with this script
  13. exclude_file="${script_dir}/excluded_files.txt"
  14. borg create \
  15. --verbose \
  16. --filter AME \
  17. --list \
  18. --stats \
  19. --show-rc \
  20. --compression lz4 \
  21. --exclude-caches \
  22. --exclude-from "$exclude_file" \
  23. ::'{hostname}-{user}-{now:%Y-%m-%dT%H:%M:%S}' \
  24. /home
  25. backup_exit=$?
  26. info "Pruning repository"
  27. # Use the `prune` subcommand to maintain 7 daily, 4 weekly and 6 monthly
  28. # archives of THIS machine. The '{hostname}-' prefix is very important to
  29. # limit prune's operation to this machine's archives and not apply to
  30. # other machines' archives also:
  31. borg prune \
  32. --list \
  33. --glob-archives '{hostname}-' \
  34. --show-rc \
  35. --keep-daily 7 \
  36. --keep-weekly 4 \
  37. --keep-monthly 6
  38. prune_exit=$?
  39. # actually free repo disk space by compacting segments
  40. info "Compacting repository"
  41. borg compact
  42. compact_exit=$?
  43. # use highest exit code as global exit code
  44. global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
  45. global_exit=$(( compact_exit > global_exit ? compact_exit : global_exit ))
  46. if [ ${global_exit} -eq 0 ]; then
  47. info "Backup, Prune, and Compact finished successfully"
  48. elif [ ${global_exit} -eq 1 ]; then
  49. info "Backup, Prune, and/or Compact finished with warnings"
  50. else
  51. info "Backup, Prune, and/or Compact finished with errors"
  52. fi
  53. exit ${global_exit}