backup-to-b2.sh.j2 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/bin/bash
  2. # {{ ansible_managed }}
  3. LOG_FILE="/var/log/rclone/rclone-$(date +%Y-%m-%d).log"
  4. {% if backups is defined and 'containers' in backups %}
  5. {% if containers is defined %}
  6. {% for container in containers %}
  7. {% if container.volumes is defined %}
  8. {% for volume in container.volumes %}
  9. {% if volume|regex_replace(':.*') not in backups_ignore %}
  10. echo "Starting backup of '{{ container.name }}:{{ volume | regex_replace(':.*') }}'." | ts >> "$LOG_FILE"
  11. /usr/bin/rclone copy {{ volume | regex_replace(':.*') }}/ b2-container-data:current/{{ ansible_hostname }}/{{ container.name }}/{{ volume | regex_replace(':.*') | basename }} \
  12. --backup-dir=b2-container-data:old/{{ ansible_hostname }}/{{ container.name }}/{{ volume | regex_replace(':.*') | basename }} \
  13. --suffix=-$(date +%Y-%m-%d) \
  14. --fast-list \
  15. --suffix-keep-extension \
  16. --exclude "*log" \
  17. --log-file="$LOG_FILE" -v
  18. if [[ $? > 0 ]]; then
  19. curl \
  20. -H "Title: rclone backup error" \
  21. -H "Priority: urgent" \
  22. -H "Tags: warning" \
  23. -d "Backup of {{ container.name }}:{{ volume | regex_replace(':.*') }} on $(hostname) had an error. Please check logs at $LOG_FILE" \
  24. https://ntfy.warchildstory.com/{{ ntfy_server_notifications }}
  25. fi
  26. echo "Finished backup of '{{ container.name }}:{{ volume | regex_replace(':.*') }}'." | ts >> "$LOG_FILE"
  27. {% endif %}
  28. {% endfor %}
  29. {% endif %}
  30. {% endfor %}
  31. {% endif %}
  32. {% endif %}
  33. {% if ansible_hostname == 'tvheadend' %}
  34. ## Photos
  35. PHOTOS_LOG="/tmp/b2-photos.log"
  36. > $PHOTOS_LOG
  37. echo "Starting backup of photos..." | ts >> "$LOG_FILE"
  38. #/usr/bin/rclone copy /mnt/nas/private/Photos b2-photos:current --backup-dir=b2-photos:old --suffix=-$(date +%Y-%m-%d) --fast-list --suffix-keep-extension --exclude "*DS_Store" --log-file=$PHOTOS_LOG | ts | tee -a "$LOG_FILE"
  39. /usr/bin/rclone copy /mnt/nas/private/Photos b2-photos:current --fast-list --immutable --exclude "*DS_Store" --log-file=$PHOTOS_LOG -v | ts | tee -a "$LOG_FILE"
  40. if [[ $? > 0 ]]; then
  41. curl \
  42. -H "Title: rclone backup error" \
  43. -H "Priority: urgent" \
  44. -H "Tags: warning" \
  45. -d "Backup of Photos on $(hostname) had an error. Please check logs at $LOG_DIR/$(basename -- $PHOTOS_LOG)" \
  46. https://ntfy.warchildstory.com/{{ ntfy_server_notifications }}
  47. fi
  48. mv "$PHOTOS_LOG" "$LOG_DIR"
  49. echo "Finished backing up photos" | ts >> "$LOG_FILE"
  50. # Data
  51. DATA_LOG="/tmp/b2-data.log"
  52. > $DATA_LOG
  53. echo "Starting backup of data..." | ts >> "$LOG_FILE"
  54. /usr/bin/rclone copy /mnt/nas/private/Data b2-data:current --backup-dir=b2-data:old --suffix=-$(date +%Y-%m-%d) --fast-list --suffix-keep-extension --exclude "*DS_Store" --log-file=$DATA_LOG -v | ts | tee -a "$LOG_FILE"
  55. if [[ $? > 0 ]]; then
  56. curl \
  57. -H "Title: rclone backup error" \
  58. -H "Priority: urgent" \
  59. -H "Tags: warning" \
  60. -d "Backup of Data on $(hostname) had an error. Please check logs at $LOG_DIR/$(basename -- $DATA_LOG)" \
  61. https://ntfy.warchildstory.com/{{ ntfy_server_notifications }}
  62. fi
  63. mv "$DATA_LOG" "$LOG_DIR"
  64. echo "Finished backing up data" | ts >> "$LOG_FILE"
  65. # Music
  66. MUSIC_LOG="/tmp/b2-music.log"
  67. > $MUSIC_LOG
  68. echo "Starting backup of music..." | ts >> "$LOG_FILE"
  69. /usr/bin/rclone copy /mnt/nas/public b2-music:current --backup-dir=b2-music:old --suffix=-$(date +%Y-%m-%d) --fast-list --suffix-keep-extension --include=Music/** --include="Music Videos"/** --log-file=$MUSIC_LOG -v | ts | tee -a "$LOG_FILE"
  70. if [[ $? > 0 ]]; then
  71. curl \
  72. -H "Title: rclone backup error" \
  73. -H "Priority: urgent" \
  74. -H "Tags: warning" \
  75. -d "Backup of Music on $(hostname) had an error. Please check logs at $LOG_DIR/$(basename -- $MUSIC_LOG)" \
  76. https://ntfy.warchildstory.com/{{ ntfy_server_notifications }}
  77. fi
  78. mv "$MUSIC_LOG" "$LOG_DIR"
  79. echo "Finished backing up music" | ts >> "$LOG_FILE"
  80. # Recipes
  81. RECIPES_LOG="/tmp/b2-recipes.log"
  82. > $RECIPES_LOG
  83. echo "Starting backup of recipes..." | ts >> "$LOG_FILE"
  84. /usr/bin/rclone copy /mnt/nas/public b2-data:current --backup-dir=b2-data:old --suffix=-$(date +%Y-%m-%d) --fast-list --suffix-keep-extension --include=Recipes/** --log-file=$RECIPES_LOG -v | ts | tee -a "$LOG_FILE"
  85. if [[ $? > 0 ]]; then
  86. curl \
  87. -H "Title: rclone backup error" \
  88. -H "Priority: urgent" \
  89. -H "Tags: warning" \
  90. -d "Backup of Recipes on $(hostname) had an error. Please check logs at $LOG_DIR/$(basename -- $RECIPES_LOG)" \
  91. https://ntfy.warchildstory.com/{{ ntfy_server_notifications }}
  92. fi
  93. mv "$RECIPES_LOG" "$LOG_DIR"
  94. echo "Finished backing up recipes" | ts >> "$LOG_FILE"
  95. {% endif %}