From 8d6f88c382b42a75f07d9089c0dce43ff2282b44 Mon Sep 17 00:00:00 2001 From: Peter Braun Date: Thu, 25 Jun 2020 14:02:05 +0200 Subject: [PATCH] fix: crw 2.0 requires backing up non-default container inside a pod --- image/tools/lib/component/codeready_pv.sh | 2 +- image/tools/lib/utils.sh | 33 +++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/image/tools/lib/component/codeready_pv.sh b/image/tools/lib/component/codeready_pv.sh index 739497f..8bedbdb 100755 --- a/image/tools/lib/component/codeready_pv.sh +++ b/image/tools/lib/component/codeready_pv.sh @@ -4,7 +4,7 @@ function dump_pod_data { workspace_pod_name=$1 dump_dest=$2 workspace_id=$(echo ${workspace_pod_name} | awk -F"." '{ print $1}') - cp_pod_data "${PRODUCT_NAMESPACE}/${workspace_pod_name}:/projects" "${dump_dest}/${workspace_id}" + cp_container_data "${workspace_pod_name}" "${PRODUCT_NAMESPACE}/${workspace_pod_name}:/projects" "${dump_dest}/${workspace_id}" } function component_dump_data { diff --git a/image/tools/lib/utils.sh b/image/tools/lib/utils.sh index bad1166..231fda6 100644 --- a/image/tools/lib/utils.sh +++ b/image/tools/lib/utils.sh @@ -17,6 +17,39 @@ function cp_pod_data { done } +# Backup every container inside a pod +function cp_container_data { + pod_name=$1 + pod_data_src=$2 + cp_dest=$3 + + # Get a list of containers inside the pod + containers=$(oc get pods "$pod_name" -ojsonpath='{.spec.containers[*].name}' -n "${PRODUCT_NAMESPACE}") + + for container in ${containers}; do + container_dest="$cp_dest-$container" + timestamp_echo "backing up container $container in pod $pod_name" + num_attempted_copy=0 + max_tries=5 + + # Disable errors because some of the containers might not have the directory to back up + set +eo pipefail + + copy_output=$(oc cp "$pod_data_src" "$container_dest" -c "$container") + # Check if any files were rewritten to during oc cp, and copy it again if it was. + while [[ $copy_output == *"file changed as we read it"* ]] && [ $num_attempted_copy -lt $max_tries ] + do + timestamp_echo "A file has been overwritten during copying, executing 'oc cp' again" + sleep 5 + copy_output=$(oc cp "$pod_data_src" "$container_dest" -c "$container") + ((num_attempted_copy++)) + done + + # Re-enable errors + set -eo pipefail + done +} + function timestamp_echo { echo `(date -u '+%Y-%m-%d %H:%M:%S')` '==>' $1 } \ No newline at end of file