backup-rotator.sh 709 B

123456789101112131415161718192021222324252627
  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 does't exist or is not a directory" 1>&2
  17. exit 1
  18. fi
  19. find "${BACKUP_PATH}/" -type f -name "gogs-backup-*.zip" -${FIND_EXPRESSION} -print -exec rm "{}" +
  20. }
  21. main "$@"