backup-rotator.sh 739 B

1234567891011121314151617181920212223242526272829
  1. #!/usr/bin/env sh
  2. # This is very simple, yet effective backup rotation script.
  3. # Using find command, all files that are older than BACKUP_RETENTION_DAYS are accumulated and deleted using rm.
  4. main() {
  5. BACKUP_PATH="${1:-}"
  6. FIND_EXPRESSION="${2:-mtime +7}"
  7. if [ -z "${BACKUP_PATH}" ]; then
  8. echo "Error: Required argument missing BACKUP_PATH" 1>&2
  9. exit 1
  10. fi
  11. if [ "$(realpath "${BACKUP_PATH}")" = "/" ]; then
  12. echo "Error: Dangerous BACKUP_PATH: /" 1>&2
  13. exit 1
  14. fi
  15. if [ ! -d "${BACKUP_PATH}" ]; then
  16. echo "Error: BACKUP_PATH doesn't exist or is not a directory" 1>&2
  17. exit 1
  18. fi
  19. # shellcheck disable=SC2086
  20. find "${BACKUP_PATH}/" -type f -name "gogs-backup-*.zip" -${FIND_EXPRESSION} -print -exec rm "{}" +
  21. }
  22. main "$@"