Skip to content

Commit 72d69d5

Browse files
committed
[PLAT-18514] YBA backups should not overwrite eachother
Summary: If a customer tries to take a yba backup with prometheus data and releases, it can take some time to complete. If they have HA setup, the HA background process will also trigger a backup, overwriting some of the files from the yba-ctl backup. To get around this, yb_platform_backup.sh now stages into a temp directory that is timestamped Test Plan: ran backup and validated new target directory is used and backups are correct Reviewers: anijhawan, nbhatia, nsingh, muthu Reviewed By: muthu Subscribers: nikhil Differential Revision: https://phorge.dev.yugabyte.com/D46632
1 parent c73590e commit 72d69d5

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

managed/devops/bin/yb_platform_backup.sh

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,25 @@ create_backup() {
448448
metadata_dir="/opt/yugabyte"
449449
target_dir="/opt/yugabyte/yugaware/data"
450450
fi
451+
# Use a timestamped subdirectory to avoid conflicts with other backups
452+
# This is nanosecond precision, highly unlikely to conflict with other backups, but we have
453+
# retries for safety.
454+
possible_target_dir="${target_dir}/backup_$(date +%s%N)"
455+
for i in {1..10}; do
456+
if [ -d "${possible_target_dir}" ]; then
457+
echo "Backup directory ${possible_target_dir} already exists, wait for 1 second and retry"
458+
sleep 1
459+
possible_target_dir="${target_dir}/backup_$(date +%s%N)"
460+
else
461+
break
462+
fi
463+
done
464+
if [ -d "${possible_target_dir}" ]; then
465+
echo "Backup directory ${possible_target_dir} already exists, exiting"
466+
exit 1
467+
fi
468+
target_dir="${possible_target_dir}"
469+
mkdir -p ${target_dir}
451470
if [[ "${yba_installer}" = true ]]; then
452471
version=$(basename $(realpath ${data_dir}/software/active))
453472
metadata_regex="**/${version}/**/yugaware/conf/${VERSION_METADATA}"
@@ -467,7 +486,7 @@ create_backup() {
467486

468487
tar_name="${output_path}/backup_${now}.tar"
469488
tgz_name="${output_path}/backup_${now}.tgz"
470-
db_backup_path="${data_dir}/${PLATFORM_DUMP_FNAME}"
489+
db_backup_path="${target_dir}/${PLATFORM_DUMP_FNAME}"
471490
trap 'delete_db_backup ${db_backup_path}' RETURN
472491
if [[ "$ybdb" = true ]]; then
473492
create_ybdb_backup "${db_backup_path}" "${db_username}" "${db_host}" "${db_port}" \
@@ -493,9 +512,9 @@ create_backup() {
493512

494513
# Backup prometheus data.
495514
if [[ "$exclude_prometheus" = false ]]; then
496-
trap 'run_sudo_cmd "rm -rf ${data_dir}/${PROMETHEUS_SNAPSHOT_DIR}"' RETURN
515+
trap 'run_sudo_cmd "rm -rf ${target_dir}/${PROMETHEUS_SNAPSHOT_DIR}"' RETURN
497516
echo "Creating prometheus snapshot..."
498-
set_prometheus_data_dir "${prometheus_host}" "${prometheus_port}" "${data_dir}" \
517+
set_prometheus_data_dir "${prometheus_host}" "${prometheus_port}" "${target_dir}" \
499518
"${prometheus_protocol}"
500519
snapshot_cmd="curl -k -X POST \
501520
${prometheus_protocol}://${prometheus_host}:${prometheus_port}/api/v1/admin/tsdb/snapshot"
@@ -504,25 +523,25 @@ create_backup() {
504523
fi
505524
snapshot_dir=$( $snapshot_cmd | ${PYTHON_EXECUTABLE} -c \
506525
"import sys, json; print(json.load(sys.stdin)['data']['name'])")
507-
mkdir -p "$data_dir/$PROMETHEUS_SNAPSHOT_DIR"
526+
mkdir -p "$target_dir/$PROMETHEUS_SNAPSHOT_DIR"
508527
run_sudo_cmd "cp -aR ${PROMETHEUS_DATA_DIR}/snapshots/${snapshot_dir} \
509-
${data_dir}/${PROMETHEUS_SNAPSHOT_DIR}"
528+
${target_dir}/${PROMETHEUS_SNAPSHOT_DIR}"
510529
run_sudo_cmd "rm -rf ${PROMETHEUS_DATA_DIR}/snapshots/${snapshot_dir}"
511530
FIND_OPTIONS+=( -o -path "**/${PROMETHEUS_SNAPSHOT_DIR}/**" )
512531
fi
513532
# Close out paths in FIND_OPTIONS with a close-paren, and add -exec
514533
FIND_OPTIONS+=( \\\) -exec tar $TAR_OPTIONS \{} + )
515534
echo "Creating platform backup package..."
516-
cd ${data_dir}
535+
cd ${target_dir}
517536

518537
eval find -L ${FIND_OPTIONS[@]}
519538

520539
gzip -9 < ${tar_name} > ${tgz_name}
521540
cleanup "${tar_name}"
522541
delete_db_backup "${db_backup_path}"
523542

524-
# Delete the version metadata backup if we had created it earlier
525-
docker_aware_cmd "yugaware" "rm -f ${data_dir}/${VERSION_METADATA_BACKUP}"
543+
# Delete the target directory now that backup is done.
544+
docker_aware_cmd "yugaware" "rm -rf ${target_dir}"
526545

527546
echo "Finished creating backup ${tgz_name}"
528547
modify_service yb-platform restart

0 commit comments

Comments
 (0)