fix: convert scripts/backup.sh line endings from CRLF to LF

This commit is contained in:
이태훈
2026-06-22 11:36:50 +09:00
parent 621b05a890
commit 6a76f6968b

View File

@@ -1,125 +1,125 @@
#!/usr/bin/env sh #!/usr/bin/env sh
set -eu set -eu
COMMAND="${1:-help}" COMMAND="${1:-help}"
ENV_FILE="${ENV_FILE:-.env}" ENV_FILE="${ENV_FILE:-.env}"
BACKUP_ROOT="${BACKUP_ROOT:-backups}" BACKUP_ROOT="${BACKUP_ROOT:-backups}"
RETENTION_DAYS="${RETENTION_DAYS:-14}" RETENTION_DAYS="${RETENTION_DAYS:-14}"
TIMESTAMP="${BACKUP_TIMESTAMP:-$(date +%Y%m%d_%H%M%S)}" TIMESTAMP="${BACKUP_TIMESTAMP:-$(date +%Y%m%d_%H%M%S)}"
log() { log() {
printf '[backup] %s\n' "$*" printf '[backup] %s\n' "$*"
} }
fail() { fail() {
printf '[backup] %s\n' "$*" >&2 printf '[backup] %s\n' "$*" >&2
exit 1 exit 1
} }
require_command() { require_command() {
command -v "$1" >/dev/null 2>&1 || fail "Required command not found: $1" command -v "$1" >/dev/null 2>&1 || fail "Required command not found: $1"
} }
has_command() { has_command() {
command -v "$1" >/dev/null 2>&1 command -v "$1" >/dev/null 2>&1
} }
load_env() { load_env() {
[ -f "$ENV_FILE" ] || fail "Env file not found: $ENV_FILE" [ -f "$ENV_FILE" ] || fail "Env file not found: $ENV_FILE"
case "$ENV_FILE" in case "$ENV_FILE" in
*/*) env_path="$ENV_FILE" ;; */*) env_path="$ENV_FILE" ;;
*) env_path="./$ENV_FILE" ;; *) env_path="./$ENV_FILE" ;;
esac esac
set -a set -a
# shellcheck disable=SC1090 # shellcheck disable=SC1090
. "$env_path" . "$env_path"
set +a set +a
: "${DB_HOST:?DB_HOST is required in $ENV_FILE}" : "${DB_HOST:?DB_HOST is required in $ENV_FILE}"
: "${DB_PORT:=3306}" : "${DB_PORT:=3306}"
: "${DB_USER:?DB_USER is required in $ENV_FILE}" : "${DB_USER:?DB_USER is required in $ENV_FILE}"
: "${DB_PASS:?DB_PASS is required in $ENV_FILE}" : "${DB_PASS:?DB_PASS is required in $ENV_FILE}"
: "${DB_NAME:?DB_NAME is required in $ENV_FILE}" : "${DB_NAME:?DB_NAME is required in $ENV_FILE}"
} }
db_dump() { db_dump() {
require_command gzip require_command gzip
load_env load_env
mkdir -p "$BACKUP_ROOT/db" mkdir -p "$BACKUP_ROOT/db"
output_path="$BACKUP_ROOT/db/${DB_NAME}_${TIMESTAMP}.sql.gz" output_path="$BACKUP_ROOT/db/${DB_NAME}_${TIMESTAMP}.sql.gz"
log "Creating DB dump: $output_path" log "Creating DB dump: $output_path"
if has_command mysqldump; then if has_command mysqldump; then
MYSQL_PWD="$DB_PASS" mysqldump \ MYSQL_PWD="$DB_PASS" mysqldump \
--host="$DB_HOST" \ --host="$DB_HOST" \
--port="$DB_PORT" \ --port="$DB_PORT" \
--user="$DB_USER" \ --user="$DB_USER" \
--single-transaction \ --single-transaction \
--quick \ --quick \
--routines \ --routines \
--triggers \ --triggers \
"$DB_NAME" | gzip > "$output_path" "$DB_NAME" | gzip > "$output_path"
elif has_command docker; then elif has_command docker; then
docker exec itam-backend sh -lc "MYSQL_PWD=\"$DB_PASS\" exec mysqldump --host=\"$DB_HOST\" --port=\"$DB_PORT\" --user=\"$DB_USER\" --single-transaction --quick --routines --triggers \"$DB_NAME\"" | gzip > "$output_path" docker exec itam-backend sh -lc "MYSQL_PWD=\"$DB_PASS\" exec mysqldump --host=\"$DB_HOST\" --port=\"$DB_PORT\" --user=\"$DB_USER\" --single-transaction --quick --routines --triggers \"$DB_NAME\"" | gzip > "$output_path"
else else
fail "Required command not found: mysqldump (and docker fallback unavailable)" fail "Required command not found: mysqldump (and docker fallback unavailable)"
fi fi
log "DB dump completed: $output_path" log "DB dump completed: $output_path"
} }
files_backup() { files_backup() {
require_command tar require_command tar
mkdir -p "$BACKUP_ROOT/files" mkdir -p "$BACKUP_ROOT/files"
archive_path="$BACKUP_ROOT/files/runtime_${TIMESTAMP}.tar.gz" archive_path="$BACKUP_ROOT/files/runtime_${TIMESTAMP}.tar.gz"
set -- set --
[ -f "$ENV_FILE" ] && set -- "$@" "$ENV_FILE" [ -f "$ENV_FILE" ] && set -- "$@" "$ENV_FILE"
[ -d "uploads" ] && set -- "$@" "uploads" [ -d "uploads" ] && set -- "$@" "uploads"
[ -f "map_config.json" ] && set -- "$@" "map_config.json" [ -f "map_config.json" ] && set -- "$@" "map_config.json"
[ "$#" -gt 0 ] || fail "No runtime files found to archive" [ "$#" -gt 0 ] || fail "No runtime files found to archive"
log "Creating runtime archive: $archive_path" log "Creating runtime archive: $archive_path"
tar -czf "$archive_path" "$@" tar -czf "$archive_path" "$@"
log "Runtime archive completed: $archive_path" log "Runtime archive completed: $archive_path"
} }
cleanup_backups() { cleanup_backups() {
require_command find require_command find
[ -d "$BACKUP_ROOT" ] || { [ -d "$BACKUP_ROOT" ] || {
log "Backup root does not exist, skipping cleanup: $BACKUP_ROOT" log "Backup root does not exist, skipping cleanup: $BACKUP_ROOT"
return 0 return 0
} }
log "Deleting backup files older than ${RETENTION_DAYS} days from $BACKUP_ROOT" log "Deleting backup files older than ${RETENTION_DAYS} days from $BACKUP_ROOT"
find "$BACKUP_ROOT" -type f -mtime "+$RETENTION_DAYS" -print -delete find "$BACKUP_ROOT" -type f -mtime "+$RETENTION_DAYS" -print -delete
} }
case "$COMMAND" in case "$COMMAND" in
db) db)
db_dump db_dump
;; ;;
files) files)
files_backup files_backup
;; ;;
full) full)
db_dump db_dump
files_backup files_backup
;; ;;
cleanup) cleanup)
cleanup_backups cleanup_backups
;; ;;
help|--help|-h) help|--help|-h)
log "Commands: db | files | full | cleanup" log "Commands: db | files | full | cleanup"
;; ;;
*) *)
fail "Unknown command: $COMMAND" fail "Unknown command: $COMMAND"
;; ;;
esac esac