22
33# Simple logging
44log () {
5- echo " [$( date ' +%H:%M:%S' ) ] $1 "
5+ echo " [$( date ' +%H:%M:%S' ) ] $1 "
66}
77
88log_error () {
9- echo " [$( date ' +%H:%M:%S' ) ] ERROR: $1 " >&2
9+ echo " [$( date ' +%H:%M:%S' ) ] ERROR: $1 " >&2
1010}
1111
1212log_step () {
13- echo " [$( date ' +%H:%M:%S' ) ] [$1 /$2 ] $3 "
13+ echo " [$( date ' +%H:%M:%S' ) ] [$1 /$2 ] $3 "
1414}
1515
1616# Parse arguments
1717while [[ $# -gt 0 ]]; do
18- case " $1 " in
19- --dsn)
20- DSN=" $2 "
21- shift 2
22- ;;
23- --stage)
24- STAGE=" $2 "
25- shift 2
26- ;;
27- * )
28- if [[ " $1 " =~ ^[0-9]{8}$ ]]; then
29- DATE_ARG=" $1 "
30- shift
31- else
32- log_error " Unknown parameter: $1 "
33- exit 1
34- fi
35- ;;
36- esac
18+ case " $1 " in
19+ --dsn)
20+ DSN=" $2 "
21+ shift 2
22+ ;;
23+ --stage)
24+ STAGE=" $2 "
25+ shift 2
26+ ;;
27+ * )
28+ if [[ " $1 " =~ ^[0-9]{8}$ ]]; then
29+ DATE_ARG=" $1 "
30+ shift
31+ else
32+ log_error " Unknown parameter: $1 "
33+ exit 1
34+ fi
35+ ;;
36+ esac
3737done
3838
3939# Validate parameters
4040if [[ -z " $STAGE " || -z " $DATE_ARG " ]]; then
41- log_error " Missing required parameters: --stage or yyyymmdd date"
42- exit 1
41+ log_error " Missing required parameters: --stage or yyyymmdd date"
42+ exit 1
4343fi
4444
4545if [[ -z " $DSN " ]]; then
46- DSN=" $BENDSQL_DSN "
47- if [[ -z " $DSN " ]]; then
48- log_error " DSN not provided and BENDSQL_DSN not set"
49- exit 1
50- fi
46+ DSN=" $BENDSQL_DSN "
47+ if [[ -z " $DSN " ]]; then
48+ log_error " DSN not provided and BENDSQL_DSN not set"
49+ exit 1
50+ fi
5151fi
5252
5353# Format date
@@ -66,8 +66,8 @@ DOWNLOAD_SQL="PRESIGN DOWNLOAD @${STAGE}/${TAR_FILE}"
6666DOWNLOAD_URL=$( bendsql --dsn " ${DSN} " --query=" ${DOWNLOAD_SQL} " | awk ' {print $3}' )
6767
6868if [[ -z " $DOWNLOAD_URL " ]]; then
69- log_error " Failed to generate download URL for ${TAR_FILE} "
70- exit 1
69+ log_error " Failed to generate download URL for ${TAR_FILE} "
70+ exit 1
7171fi
7272log " Download URL generated successfully"
7373
@@ -76,8 +76,8 @@ log_step "2" "6" "Downloading ${TAR_FILE} from stage @${STAGE}"
7676curl -s -o " ${TAR_FILE} " " ${DOWNLOAD_URL} "
7777
7878if [[ ! -f " ${TAR_FILE} " ]]; then
79- log_error " Failed to download ${TAR_FILE} "
80- exit 1
79+ log_error " Failed to download ${TAR_FILE} "
80+ exit 1
8181fi
8282
8383FILE_SIZE=$( du -h " ${TAR_FILE} " | cut -f1)
@@ -98,21 +98,21 @@ TARGET_DIRS=("columns" "user_functions" "query_raw_logs" "query_logs" "query_pro
9898PREFIX=" "
9999
100100for target_dir in " ${TARGET_DIRS[@]} " ; do
101- SAMPLE_FILE=$( find " ${TEMP_DIR} " -path " */${target_dir} /*" -type f | head -1)
102- if [[ -n " $SAMPLE_FILE " ]]; then
103- RELATIVE_PATH=" ${SAMPLE_FILE# ${TEMP_DIR} / } "
104- PREFIX=$( echo " $RELATIVE_PATH " | sed " s|/${target_dir} /.*||" | sed " s|${target_dir} /.*||" )
105- if [[ -n " $PREFIX " ]]; then
106- PREFIX=" ${PREFIX} /"
107- fi
108- break
109- fi
101+ SAMPLE_FILE=$( find " ${TEMP_DIR} " -path " */${target_dir} /*" -type f | head -1)
102+ if [[ -n " $SAMPLE_FILE " ]]; then
103+ RELATIVE_PATH=" ${SAMPLE_FILE# ${TEMP_DIR} / } "
104+ PREFIX=$( echo " $RELATIVE_PATH " | sed " s|/${target_dir} /.*||" | sed " s|${target_dir} /.*||" )
105+ if [[ -n " $PREFIX " ]]; then
106+ PREFIX=" ${PREFIX} /"
107+ fi
108+ break
109+ fi
110110done
111111
112112if [[ -n " $PREFIX " ]]; then
113- log " Path prefix detected: '${PREFIX} ' - will be stripped during upload"
113+ log " Path prefix detected: '${PREFIX} ' - will be stripped during upload"
114114else
115- log " No path prefix detected - using original file paths"
115+ log " No path prefix detected - using original file paths"
116116fi
117117
118118# Step 5: Upload files
@@ -129,32 +129,32 @@ UPLOAD_SUCCESS=0
129129UPLOAD_FAILED=0
130130
131131find " ${TEMP_DIR} " -type f | while read -r FILE; do
132- CURRENT_FILE=$(( CURRENT_FILE + 1 ))
133- RELATIVE_PATH=" ${FILE# ${TEMP_DIR} / } "
134-
135- if [[ -n " $PREFIX " && " $RELATIVE_PATH " == ${PREFIX} * ]]; then
136- UPLOAD_PATH=" ${RELATIVE_PATH# ${PREFIX} } "
137- else
138- UPLOAD_PATH=" $RELATIVE_PATH "
139- fi
140-
141- printf " \rUploading: %d/%d files (Success: %d, Failed: %d)" " $CURRENT_FILE " " $TOTAL_FILES " " $UPLOAD_SUCCESS " " $UPLOAD_FAILED "
142-
143- UPLOAD_SQL=" PRESIGN UPLOAD @${UPLOAD_STAGE} /${UPLOAD_PATH} "
144- UPLOAD_URL=$( bendsql --dsn " ${DSN} " --query=" ${UPLOAD_SQL} " | awk ' {print $3}' )
145-
146- if [[ -n " $UPLOAD_URL " ]]; then
147- if curl -s -X PUT -T " ${FILE} " " ${UPLOAD_URL} " ; then
148- UPLOAD_SUCCESS=$(( UPLOAD_SUCCESS + 1 ))
149- else
150- UPLOAD_FAILED=$(( UPLOAD_FAILED + 1 ))
151- fi
152- else
153- UPLOAD_FAILED=$(( UPLOAD_FAILED + 1 ))
154- fi
132+ CURRENT_FILE=$(( CURRENT_FILE + 1 ))
133+ RELATIVE_PATH=" ${FILE# ${TEMP_DIR} / } "
134+
135+ if [[ -n " $PREFIX " && " $RELATIVE_PATH " == ${PREFIX} * ]]; then
136+ UPLOAD_PATH=" ${RELATIVE_PATH# ${PREFIX} } "
137+ else
138+ UPLOAD_PATH=" $RELATIVE_PATH "
139+ fi
140+
141+ printf " \rUploading: %d/%d files (Success: %d, Failed: %d)" " $CURRENT_FILE " " $TOTAL_FILES " " $UPLOAD_SUCCESS " " $UPLOAD_FAILED "
142+
143+ UPLOAD_SQL=" PRESIGN UPLOAD @${UPLOAD_STAGE} /${UPLOAD_PATH} "
144+ UPLOAD_URL=$( bendsql --dsn " ${DSN} " --query=" ${UPLOAD_SQL} " | awk ' {print $3}' )
145+
146+ if [[ -n " $UPLOAD_URL " ]]; then
147+ if curl -s -X PUT -T " ${FILE} " " ${UPLOAD_URL} " ; then
148+ UPLOAD_SUCCESS=$(( UPLOAD_SUCCESS + 1 ))
149+ else
150+ UPLOAD_FAILED=$(( UPLOAD_FAILED + 1 ))
151+ fi
152+ else
153+ UPLOAD_FAILED=$(( UPLOAD_FAILED + 1 ))
154+ fi
155155done
156156
157- echo # New line after progress
157+ echo # New line after progress
158158log " Upload completed: ${UPLOAD_SUCCESS} successful, ${UPLOAD_FAILED} failed"
159159
160160# Cleanup
@@ -171,25 +171,25 @@ log "Created database: ${RESTORE_DATABASE}"
171171
172172# Restore tables
173173declare -A TABLE_MAP=(
174- [" columns" ]=" system.columns:columns"
175- [" user_functions" ]=" system.user_functions:user_functions"
176- [" log_history" ]=" system_history.log_history:query_raw_logs"
177- [" query_history" ]=" system_history.query_history:query_logs"
178- [" profile_history" ]=" system_history.profile_history:query_profile_logs"
174+ [" columns" ]=" system.columns:columns"
175+ [" user_functions" ]=" system.user_functions:user_functions"
176+ [" log_history" ]=" system_history.log_history:query_raw_logs"
177+ [" query_history" ]=" system_history.query_history:query_logs"
178+ [" profile_history" ]=" system_history.profile_history:query_profile_logs"
179179)
180180
181181for table_name in " ${! TABLE_MAP[@]} " ; do
182- IFS=' :' read -r source_table source_path <<< " ${TABLE_MAP[$table_name]}"
183-
184- log " Restoring table: ${RESTORE_DATABASE} .${table_name} from @${UPLOAD_STAGE} /${source_path} "
185-
186- bendsql --dsn " ${DSN} " --database " ${RESTORE_DATABASE} " --query=" CREATE TABLE ${table_name} LIKE ${source_table} ;" > /dev/null 2>&1
187- bendsql --dsn " ${DSN} " --database " ${RESTORE_DATABASE} " --query=" COPY INTO ${table_name} FROM @${UPLOAD_STAGE} /${source_path} ;" > /dev/null 2>&1
188-
189- ROW_COUNT=$( bendsql --dsn " ${DSN} " --database " ${RESTORE_DATABASE} " --query=" SELECT COUNT(*) FROM ${table_name} ;" | tail -1)
190- log " Table ${table_name} restored: ${ROW_COUNT} rows"
182+ IFS=' :' read -r source_table source_path <<< " ${TABLE_MAP[$table_name]}"
183+
184+ log " Restoring table: ${RESTORE_DATABASE} .${table_name} from @${UPLOAD_STAGE} /${source_path} "
185+
186+ bendsql --dsn " ${DSN} " --database " ${RESTORE_DATABASE} " --query=" CREATE TABLE ${table_name} LIKE ${source_table} ;" > /dev/null 2>&1
187+ bendsql --dsn " ${DSN} " --database " ${RESTORE_DATABASE} " --query=" COPY INTO ${table_name} FROM @${UPLOAD_STAGE} /${source_path} ;" > /dev/null 2>&1
188+
189+ ROW_COUNT=$( bendsql --dsn " ${DSN} " --database " ${RESTORE_DATABASE} " --query=" SELECT COUNT(*) FROM ${table_name} ;" | tail -1)
190+ log " Table ${table_name} restored: ${ROW_COUNT} rows"
191191done
192192
193193log " Log restoration completed successfully"
194194log " Restored database: ${RESTORE_DATABASE} "
195- log " Tables available: columns, user_functions, log_history, query_history, profile_history"
195+ log " Tables available: columns, user_functions, log_history, query_history, profile_history"
0 commit comments