backup-init.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/usr/bin/env bash
  2. set -e
  3. BACKUP_PATH="/backup"
  4. # Make sure that required directories exist
  5. mkdir -p "${BACKUP_PATH}"
  6. mkdir -p "/etc/crontabs"
  7. chown git:git /backup
  8. chmod 2770 /backup
  9. # [string] BACKUP_INTERVAL Period expression
  10. # [string] BACKUP_RETENTION Period expression
  11. if [ -z "${BACKUP_INTERVAL}" ]; then
  12. echo "Backup disabled: BACKUP_INTERVAL has not been found" 1>&2
  13. exit 1
  14. fi
  15. if [ -z "${BACKUP_RETENTION}" ]; then
  16. echo "Backup retention period is not defined, default to 7 days" 1>&2
  17. BACKUP_RETENTION='7d'
  18. fi
  19. # Parse BACKUP_INTERVAL environment variable and generate appropriate cron expression. Backup cron task will be run as scheduled.
  20. # Expected format: nu (n - number, u - unit) (eg. 3d means 3 days)
  21. # Supported units: h - hours, d - days, M - months
  22. parse_generate_cron_expression() {
  23. CRON_EXPR_MINUTES="*"
  24. CRON_EXPR_HOURS="*"
  25. CRON_EXPR_DAYS="*"
  26. CRON_EXPR_MONTHS="*"
  27. TIME_INTERVAL=$(echo "${BACKUP_INTERVAL}" | sed -e 's/[hdM]$//')
  28. TIME_UNIT=$(echo "${BACKUP_INTERVAL}" | sed -e 's/^[0-9]\+//')
  29. if [ "${TIME_UNIT}" = "h" ]; then
  30. if [ ! "${TIME_INTERVAL}" -le 23 ]; then
  31. echo "Parse error: Time unit 'h' (hour) cannot be greater than 23" 1>&2
  32. exit 1
  33. fi
  34. CRON_EXPR_MINUTES=0
  35. CRON_EXPR_HOURS="*/${TIME_INTERVAL}"
  36. elif [ "${TIME_UNIT}" = "d" ]; then
  37. if [ ! "${TIME_INTERVAL}" -le 30 ]; then
  38. echo "Parse error: Time unit 'd' (day) cannot be greater than 30" 1>&2
  39. exit 1
  40. fi
  41. CRON_EXPR_MINUTES=0
  42. CRON_EXPR_HOURS=0
  43. CRON_EXPR_DAYS="*/${TIME_INTERVAL}"
  44. elif [ "${TIME_UNIT}" = "M" ]; then
  45. if [ ! "${TIME_INTERVAL}" -le 12 ]; then
  46. echo "Parse error: Time unit 'M' (month) cannot be greater than 12" 1>&2
  47. exit 1
  48. fi
  49. CRON_EXPR_MINUTES=0
  50. CRON_EXPR_HOURS=0
  51. CRON_EXPR_DAYS="1"
  52. CRON_EXPR_MONTHS="*/${TIME_INTERVAL}"
  53. else
  54. echo "Parse error: BACKUP_INTERVAL expression is invalid" 1>&2
  55. exit 1
  56. fi
  57. echo "${CRON_EXPR_MINUTES} ${CRON_EXPR_HOURS} ${CRON_EXPR_DAYS} ${CRON_EXPR_MONTHS} *"
  58. }
  59. # Parse BACKUP_RETENTION environment variable and generate appropriate find command expression.
  60. # Expected format: nu (n - number, u - unit) (eg. 3d means 3 days)
  61. # Supported units: m - minutes, d - days
  62. parse_generate_retention_expression() {
  63. FIND_TIME_EXPR='mtime'
  64. TIME_INTERVAL=$(echo "${BACKUP_RETENTION}" | sed -e 's/[mhdM]$//')
  65. TIME_UNIT=$(echo "${BACKUP_RETENTION}" | sed -e 's/^[0-9]\+//')
  66. if [ "${TIME_UNIT}" = "m" ]; then
  67. if [ "${TIME_INTERVAL}" -le 59 ]; then
  68. echo "Warning: Minimal retention is 60m. Value set to 60m" 1>&2
  69. TIME_INTERVAL=60
  70. fi
  71. FIND_TIME_EXPR="mmin"
  72. elif [ "${TIME_UNIT}" = "h" ]; then
  73. echo "Error: Unsupported expression - Try: eg. 120m for 2 hours." 1>&2
  74. exit 1
  75. elif [ "${TIME_UNIT}" = "d" ]; then
  76. FIND_TIME_EXPR="mtime"
  77. elif [ "${TIME_UNIT}" = "M" ]; then
  78. echo "Error: Unsupported expression - Try: eg. 60d for 2 months." 1>&2
  79. exit 1
  80. else
  81. echo "Parse error: BACKUP_RETENTION expression is invalid" 1>&2
  82. exit 1
  83. fi
  84. echo "${FIND_TIME_EXPR} +${TIME_INTERVAL:-7}"
  85. }
  86. add_backup_cronjob() {
  87. CRONTAB_USER="${1:-git}"
  88. CRONTAB_FILE="/etc/crontabs/${CRONTAB_USER}"
  89. CRONJOB_EXPRESSION="${2:-}"
  90. CRONJOB_EXECUTOR="${3:-}"
  91. CRONJOB_EXECUTOR_ARGUMENTS="${4:-}"
  92. CRONJOB_TASK="${CRONJOB_EXPRESSION} /bin/sh ${CRONJOB_EXECUTOR} ${CRONJOB_EXECUTOR_ARGUMENTS}"
  93. if [ -f "${CRONTAB_FILE}" ]; then
  94. CRONJOB_EXECUTOR_COUNT=$(grep -c "${CRONJOB_EXECUTOR}" "${CRONTAB_FILE}" || exit 0)
  95. if [ "${CRONJOB_EXECUTOR_COUNT}" != "0" ]; then
  96. echo "Cron job already exists for ${CRONJOB_EXECUTOR}. Updating existing." 1>&2
  97. CRONJOB_TASK=$(echo "{CRONJOB_TASK}" | sed 's/\//\\\//g' )
  98. CRONJOB_EXECUTOR=$(echo "{CRONJOB_EXECUTOR}" | sed 's/\//\\\//g' )
  99. sed -i "/${CRONJOB_EXECUTOR}/c\\${CRONJOB_TASK}" "${CRONTAB_FILE}"
  100. return 0
  101. fi
  102. fi
  103. # Finally append new line with cron task expression
  104. echo "${CRONJOB_TASK}" >>"${CRONTAB_FILE}"
  105. }
  106. CRONTAB_USER=$(awk -v val="${PUID}" -F ":" '$3==val{print $1}' /etc/passwd)
  107. # Up to this point, it was desirable that interpreter handles the command errors and halts execution upon any error.
  108. # From now, we handle the errors our self.
  109. set +e
  110. RETENTION_EXPRESSION="$(parse_generate_retention_expression)"
  111. if [ -z "${RETENTION_EXPRESSION}" ]; then
  112. echo "Couldn't generate backup retention expression. Aborting backup setup" 1>&2
  113. exit 1
  114. fi
  115. # Backup rotator cron will run every 5 minutes
  116. add_backup_cronjob "${CRONTAB_USER}" "*/5 * * * *" "/app/gogs/docker/runtime/backup-rotator.sh" "'${BACKUP_PATH}' '${RETENTION_EXPRESSION}'"
  117. add_backup_cronjob "${CRONTAB_USER}" "$(parse_generate_cron_expression)" "/app/gogs/docker/runtime/backup-job.sh" "'${BACKUP_PATH}'"