Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. ## Configuration
  4.  
  5. # Backup
  6. backup_host=___HOST___ # e.g. hostname.local, 192.168.123.45
  7. backup_host_user=___USERNAME__ # e.g. borg
  8. backup_path=___PATH___ # e.g. /mnt/backup/Borg
  9. backup_repo=___REPO___ # e.g. default
  10. backup_passcommand=___COMMAND___ # e.g. "pass show backup"
  11.  
  12. # Notifications
  13. notify_user=___USERNAME___ # e.g. myname
  14. notify_uid=___USER_ID___ # e.g. 1000
  15.  
  16. ## Backup
  17.  
  18. print_info() { printf "\n%s %s\n\n" "$( date '+%a, %Y-%m-%d %H:%M:%S' )" "$*" >&2; }
  19.  
  20. # Check if the backup host is up
  21. host_up=`ping -c 1 $backup_host &> /dev/null; echo $?`
  22. if [ $host_up = 0 ]; then
  23. # Setting this, so the repo does not need to be given on the commandline:
  24. export BORG_REPO=ssh://$backup_host_user@$backup_host$backup_path/$backup_repo
  25.  
  26. # Setting this, so you won't be asked for your repository passphrase:
  27. #export BORG_PASSPHRASE='XYZl0ngandsecurepa_55_phrasea&&123'
  28. # or this to ask an external program to supply the passphrase:
  29. export BORG_PASSCOMMAND=$backup_passcommand
  30.  
  31. # some helpers and error handling:
  32. trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM
  33.  
  34. print_info "Starting backup"
  35.  
  36. # Backup the most important directories into an archive named after
  37. # the machine this script is currently running on:
  38.  
  39. borg create \
  40. --verbose \
  41. --filter AME \
  42. --list \
  43. --stats \
  44. --show-rc \
  45. --compression lz4 \
  46. --exclude-caches \
  47. --exclude '/home/*/.cache/*' \
  48. --exclude '/var/cache/*' \
  49. --exclude '/var/tmp/*' \
  50. \
  51. ::'{hostname}-{now}' \
  52. /etc \
  53. /home \
  54. /root \
  55. /var \
  56.  
  57. backup_exit=$?
  58.  
  59. print_info "Pruning repository"
  60.  
  61. # Use the `prune` subcommand to maintain 7 daily, 4 weekly and 6 monthly
  62. # archives of THIS machine. The '{hostname}-' prefix is very important to
  63. # limit prune's operation to this machine's archives and not apply to
  64. # other machines' archives also:
  65.  
  66. borg prune \
  67. --list \
  68. --prefix '{hostname}-' \
  69. --show-rc \
  70. --keep-hourly 24 \
  71. --keep-daily 7 \
  72. --keep-weekly 4 \
  73. --keep-monthly 6 \
  74.  
  75. prune_exit=$?
  76.  
  77. # use highest exit code as global exit code
  78. global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
  79.  
  80. if [ ${global_exit} -eq 0 ]; then
  81. notify_message="Backup and Prune finished successfully."
  82. else
  83. notify_icon=dialog-warning
  84. notify_level=critical
  85.  
  86. if [ ${global_exit} -eq 1 ]; then
  87. notify_message="Backup and/or Prune finished with warnings."
  88. else
  89. notify_message="Backup and/or Prune finished with errors."
  90. fi
  91.  
  92. sudo -u $notify_user DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$notify_uid/bus notify-send 'BorgBackup' "$notify_message" --icon=$notify_icon --urgency=$notify_level
  93. fi
  94. else
  95. global_exit=$host_up
  96. notify_message="Host is down. Skipped backup."
  97. fi
  98.  
  99. print_info "${notify_message}"
  100.  
  101. exit ${global_exit}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement