2
2
3
3
# Simple logging
4
4
log () {
5
- echo " [$( date ' +%H:%M:%S' ) ] $1 "
5
+ echo " [$( date ' +%H:%M:%S' ) ] $1 "
6
6
}
7
7
8
8
log_error () {
9
- echo " [$( date ' +%H:%M:%S' ) ] ERROR: $1 " >&2
9
+ echo " [$( date ' +%H:%M:%S' ) ] ERROR: $1 " >&2
10
10
}
11
11
12
12
log_step () {
13
- echo " [$( date ' +%H:%M:%S' ) ] [$1 /$2 ] $3 "
13
+ echo " [$( date ' +%H:%M:%S' ) ] [$1 /$2 ] $3 "
14
14
}
15
15
16
16
# Parse arguments
17
17
while [[ $# -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
37
37
done
38
38
39
39
# Validate parameters
40
40
if [[ -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
43
43
fi
44
44
45
45
if [[ -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
51
51
fi
52
52
53
53
# Format date
@@ -66,8 +66,8 @@ DOWNLOAD_SQL="PRESIGN DOWNLOAD @${STAGE}/${TAR_FILE}"
66
66
DOWNLOAD_URL=$( bendsql --dsn " ${DSN} " --query=" ${DOWNLOAD_SQL} " | awk ' {print $3}' )
67
67
68
68
if [[ -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
71
71
fi
72
72
log " Download URL generated successfully"
73
73
@@ -76,8 +76,8 @@ log_step "2" "6" "Downloading ${TAR_FILE} from stage @${STAGE}"
76
76
curl -s -o " ${TAR_FILE} " " ${DOWNLOAD_URL} "
77
77
78
78
if [[ ! -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
81
81
fi
82
82
83
83
FILE_SIZE=$( du -h " ${TAR_FILE} " | cut -f1)
@@ -98,21 +98,21 @@ TARGET_DIRS=("columns" "user_functions" "query_raw_logs" "query_logs" "query_pro
98
98
PREFIX=" "
99
99
100
100
for 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
110
110
done
111
111
112
112
if [[ -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"
114
114
else
115
- log " No path prefix detected - using original file paths"
115
+ log " No path prefix detected - using original file paths"
116
116
fi
117
117
118
118
# Step 5: Upload files
@@ -129,32 +129,32 @@ UPLOAD_SUCCESS=0
129
129
UPLOAD_FAILED=0
130
130
131
131
find " ${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
155
155
done
156
156
157
- echo # New line after progress
157
+ echo # New line after progress
158
158
log " Upload completed: ${UPLOAD_SUCCESS} successful, ${UPLOAD_FAILED} failed"
159
159
160
160
# Cleanup
@@ -171,25 +171,25 @@ log "Created database: ${RESTORE_DATABASE}"
171
171
172
172
# Restore tables
173
173
declare -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"
179
179
)
180
180
181
181
for 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"
191
191
done
192
192
193
193
log " Log restoration completed successfully"
194
194
log " 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