backup-to-b2.sh.j2 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/bin/bash
  2. # {{ ansible_managed }}
  3. LOG_FILE="/var/log/rclone/rclone-$(date +%Y-%m-%d).log"
  4. rm -f /opt/backups/*
  5. {% if backups is defined and 'containers' in backups %}
  6. {% if containers is defined %}
  7. {% if "chloe-testnas" in ansible_hostname %}
  8. echo "Stopping VPN." | ts >> "$LOG_FILE"
  9. nordvpn d
  10. {% endif %}
  11. {% for container in containers %}
  12. {% if container.volumes is defined %}
  13. {% for volume in container.volumes %}
  14. {% if volume|regex_replace(':.*') not in backups_ignore %}
  15. echo "Stopping '{{ container.name }}' service..." | ts >> "$LOG_FILE"
  16. systemctl stop {{ container.name }}
  17. echo "Service '{{ container.name }}' stopped." | ts >> "$LOG_FILE"
  18. echo "Starting backup of '{{ container.name }}:{{ volume | regex_replace(':.*') }}'." | ts >> "$LOG_FILE"
  19. tar -czf {{ backup_tmp_dir }}/$(date +%Y-%m-%d)_{{ ansible_hostname }}_{{ container.name}}_{{ volume | regex_replace(':.*') | basename }}.tar.gz {{ volume | regex_replace(':.*') }}/
  20. if [[ $? > 0 ]]; then
  21. curl \
  22. -H "Title: rclone backup error" \
  23. -H "Priority: urgent" \
  24. -H "Tags: warning" \
  25. -d "Backup of {{ container.name }}:{{ volume | regex_replace(':.*') }} on $(hostname) had an error. Please check logs at $LOG_FILE" \
  26. https://ntfy.warchildstory.com/{{ ntfy_server_notifications }}
  27. fi
  28. echo "Finished backup of '{{ container.name }}:{{ volume | regex_replace(':.*') }}'." | ts >> "$LOG_FILE"
  29. {% if "chloe-testnas" not in ansible_hostname %}
  30. echo "Starting '{{ container.name }}' service..." | ts >> "$LOG_FILE"
  31. systemctl start {{ container.name }}
  32. echo "Service '{{ container.name }}' started." | ts >> "$LOG_FILE"
  33. {% endif %}
  34. {% endif %}
  35. {% endfor %}
  36. {% endif %}
  37. {% endfor %}
  38. echo "Backing up tarballs to B2..." | ts >> "$LOG_FILE"
  39. /usr/bin/rclone copy {{ backup_tmp_dir }} b2-container-data:{{ ansible_hostname }} \
  40. --fast-list \
  41. --log-file="$LOG_FILE" -v
  42. echo "Finished backing up tarballs to B2." | ts >> "$LOG_FILE"
  43. {% if "chloe-testnas" in ansible_hostname %}
  44. echo "Starting VPN..." | ts >> "$LOG_FILE"
  45. nordvpn c
  46. {% for container in containers %}
  47. echo "Starting {{ container.name }}." | ts >> "$LOG_FILE"
  48. systemctl start {{ container.name }}
  49. {% endfor %}
  50. {% endif %}
  51. {% endif %}
  52. {% endif %}
  53. {% if ansible_hostname == 'tvheadend' %}
  54. ## Photos
  55. PHOTOS_LOG="/tmp/b2-photos.log"
  56. > $PHOTOS_LOG
  57. echo "Starting backup of photos..." | ts >> "$LOG_FILE"
  58. #/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"
  59. /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"
  60. if [[ $? > 0 ]]; then
  61. curl \
  62. -H "Title: rclone backup error" \
  63. -H "Priority: urgent" \
  64. -H "Tags: warning" \
  65. -d "Backup of Photos on $(hostname) had an error. Please check logs at $LOG_DIR/$(basename -- $PHOTOS_LOG)" \
  66. https://ntfy.warchildstory.com/{{ ntfy_server_notifications }}
  67. fi
  68. mv "$PHOTOS_LOG" "$LOG_DIR"
  69. echo "Finished backing up photos" | ts >> "$LOG_FILE"
  70. # Data
  71. DATA_LOG="/tmp/b2-data.log"
  72. > $DATA_LOG
  73. echo "Starting backup of data..." | ts >> "$LOG_FILE"
  74. /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"
  75. if [[ $? > 0 ]]; then
  76. curl \
  77. -H "Title: rclone backup error" \
  78. -H "Priority: urgent" \
  79. -H "Tags: warning" \
  80. -d "Backup of Data on $(hostname) had an error. Please check logs at $LOG_DIR/$(basename -- $DATA_LOG)" \
  81. https://ntfy.warchildstory.com/{{ ntfy_server_notifications }}
  82. fi
  83. mv "$DATA_LOG" "$LOG_DIR"
  84. echo "Finished backing up data" | ts >> "$LOG_FILE"
  85. # Music
  86. MUSIC_LOG="/tmp/b2-music.log"
  87. > $MUSIC_LOG
  88. echo "Starting backup of music..." | ts >> "$LOG_FILE"
  89. /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"
  90. if [[ $? > 0 ]]; then
  91. curl \
  92. -H "Title: rclone backup error" \
  93. -H "Priority: urgent" \
  94. -H "Tags: warning" \
  95. -d "Backup of Music on $(hostname) had an error. Please check logs at $LOG_DIR/$(basename -- $MUSIC_LOG)" \
  96. https://ntfy.warchildstory.com/{{ ntfy_server_notifications }}
  97. fi
  98. mv "$MUSIC_LOG" "$LOG_DIR"
  99. echo "Finished backing up music" | ts >> "$LOG_FILE"
  100. # Recipes
  101. RECIPES_LOG="/tmp/b2-recipes.log"
  102. > $RECIPES_LOG
  103. echo "Starting backup of recipes..." | ts >> "$LOG_FILE"
  104. /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"
  105. if [[ $? > 0 ]]; then
  106. curl \
  107. -H "Title: rclone backup error" \
  108. -H "Priority: urgent" \
  109. -H "Tags: warning" \
  110. -d "Backup of Recipes on $(hostname) had an error. Please check logs at $LOG_DIR/$(basename -- $RECIPES_LOG)" \
  111. https://ntfy.warchildstory.com/{{ ntfy_server_notifications }}
  112. fi
  113. mv "$RECIPES_LOG" "$LOG_DIR"
  114. echo "Finished backing up recipes" | ts >> "$LOG_FILE"
  115. {% endif %}