Playbooks to a new Lilik
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
4.1 KiB

  1. #!/bin/bash
  2. REPO="backup@{{ item[1].ansible_host }}.dmz.{{ domain }}:{{ item[0].key }}"
  3. export BORG_RSH="ssh -i /root/.ssh/id_ed25519_BORG"
  4. export BORG_PASSPHRASE="{{ item[1].encryption_passphrase | d('') }}"
  5. export BORG_RELOCATED_REPO_ACCESS_IS_OK="no"
  6. export BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK="no"
  7. borg --version
  8. borg break-lock backup@{{ item[1].ansible_host }}.dmz.{{ domain }}:{{ item[0].key }}
  9. {% for folder in item[0].value.folders|d({})|dict2items %}
  10. ##### Folder {{ folder.key }}
  11. BEGIN_EPOCH=$(date +%s)
  12. DATE="folder-{{ folder.key }}-$(date --iso-8601)-$(hostname)"
  13. echo "Starting backup for $DATE"
  14. borg create \
  15. --verbose \
  16. --filter AME \
  17. --list \
  18. --stats \
  19. --show-rc \
  20. --compression lz4 \
  21. --exclude-caches \
  22. {% for exclude in folder.value.excludes|d([]) %}
  23. --exclude '{{ exclude }}' \
  24. {% endfor %}
  25. \
  26. ${REPO}::'folder-{{ folder.key }}-{hostname}-{now:%Y-%m-%d@%H:%M}' \
  27. {{ folder.value.path }}
  28. backup_rc=$?
  29. prune_rc=99
  30. if [ "${backup_rc}" = "2" ]; then
  31. echo "Failed backup for $DATE"
  32. else
  33. echo "Completed backup for $DATE"
  34. echo "Pruning archives for $DATE"
  35. borg prune \
  36. --list \
  37. --prefix 'folder-{{ folder.key }}-{hostname}-' \
  38. --show-rc \
  39. --keep-daily {{ folder.value.daily|d(7) }} \
  40. --keep-weekly {{ folder.value.weekly|d(4) }} \
  41. --keep-monthly {{ folder.value.monthly|d(6) }} \
  42. ${REPO}
  43. prune_rc=$?
  44. if [ "${prune_rc}" = "2" ]; then
  45. echo "Failed pruning for $DATE"
  46. else
  47. echo "Completed pruning for $DATE"
  48. fi
  49. fi
  50. echo "$(date +%s)|${BEGIN_EPOCH}|${backup_rc}|${prune_rc}" > /var/log/backup-status/{{ item[0].key }}.{{ item[1].ansible_host }}/folder-{{ folder.key }}
  51. {% endfor %}
  52. #####
  53. {% for db in item[0].value.pgsql_dbs|d({})|dict2items %}
  54. ##### pgSQL DB {{ db.key }}
  55. DATE="pgsqldb-{{ db.key }}-$(date --iso-8601)-$(hostname)"
  56. echo "Starting backup for $DATE"
  57. su postgres -c 'pg_dump -d {{ db.value.dbname }}' | borg create \
  58. --verbose \
  59. --filter AME \
  60. --list \
  61. --stats \
  62. --show-rc \
  63. --compression lz4 \
  64. --stdin-name "{{ db.value.dbname }}.sql" \
  65. \
  66. ${REPO}::'pgsqldb-{{ db.key }}-{hostname}-{now:%Y-%m-%d@%H:%M}' \
  67. -
  68. backup_rc=$?
  69. prune_rc=99
  70. if [ "${backup_rc}" = "2" ]; then
  71. echo "Failed backup for $DATE"
  72. else
  73. echo "Completed backup for $DATE"
  74. echo "Pruning archives for $DATE"
  75. borg prune \
  76. --list \
  77. --prefix 'pgsqldb-{{ db.key }}-{hostname}-' \
  78. --show-rc \
  79. --keep-daily {{ db.value.daily|d(7) }} \
  80. --keep-weekly {{ db.value.weekly|d(4) }} \
  81. --keep-monthly {{ db.value.monthly|d(6) }} \
  82. ${REPO}
  83. prune_rc=$?
  84. if [ "${prune_rc}" = "2" ]; then
  85. echo "Failed pruning for $DATE"
  86. else
  87. echo "Completed pruning for $DATE"
  88. fi
  89. fi
  90. echo "$(date +%s)|${BEGIN_EPOCH}|${backup_rc}|${prune_rc}" > /var/log/backup-status/{{ item[0].key }}.{{ item[1].ansible_host }}/pgsqldb-{{ db.key }}
  91. {% endfor %}
  92. {% for db in item[0].value.ldap_dbs|d({})|dict2items %}
  93. ##### LDAP DB {{ db.key }}
  94. DATE="ldapdb-{{ db.key }}-$(date --iso-8601)-$(hostname)"
  95. echo "Starting backup for $DATE"
  96. slapcat -n {{ db.value.dbnum }} | borg create \
  97. --verbose \
  98. --filter AME \
  99. --list \
  100. --stats \
  101. --show-rc \
  102. --compression lz4 \
  103. --stdin-name "slapcat.db{{ db.value.dbnum }}.ldif" \
  104. \
  105. ${REPO}::'ldapdb-{{ db.key }}-{hostname}-{now:%Y-%m-%d@%H:%M}' \
  106. -
  107. backup_rc=$?
  108. prune_rc=99
  109. if [ "${backup_rc}" = "2" ]; then
  110. echo "Failed backup for $DATE"
  111. else
  112. echo "Completed backup for $DATE"
  113. echo "Pruning archives for $DATE"
  114. borg prune \
  115. --list \
  116. --prefix 'ldapdb-{{ db.key }}-{hostname}-' \
  117. --show-rc \
  118. --keep-daily {{ db.value.daily|d(7) }} \
  119. --keep-weekly {{ db.value.weekly|d(4) }} \
  120. --keep-monthly {{ db.value.monthly|d(6) }} \
  121. ${REPO}
  122. prune_rc=$?
  123. if [ "${prune_rc}" = "2" ]; then
  124. echo "Failed pruning for $DATE"
  125. else
  126. echo "Completed pruning for $DATE"
  127. fi
  128. fi
  129. echo "$(date +%s)|${BEGIN_EPOCH}|${backup_rc}|${prune_rc}" > /var/log/backup-status/{{ item[0].key }}.{{ item[1].ansible_host }}/ldapdb-{{ db.key }}
  130. {% endfor %}