Skip to content

Commit

Permalink
align sonic vm backup script with dell_sonic (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
hellt authored Aug 22, 2024
1 parent 86ddb80 commit cbba711
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions sonic/docker/backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ handle_args() {
password=$DEFAULT_PASSWORD
fi

SSH_CMD="sshpass -p $password ssh -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -p 2022"
SCP_CMD="sshpass -p $password scp -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -P 2022"
HOST="$user@127.0.0.1"
SSH_CMD="sshpass -p $password ssh -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
SCP_CMD="sshpass -p $password scp -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
HOST="$user@localhost"

# Parse commands
case $1 in
Expand Down Expand Up @@ -66,17 +66,41 @@ usage() {
}

backup() {
echo "Backing up..."
$SCP_CMD $HOST:$REMOTE_FILE $BACKUP_FILE
echo "Retrieveing the config from the VM..."
# copy the original config to the tmp location and set permissions to 777
# and then copy out the file from the temp location
$SSH_CMD $HOST "sudo cp $REMOTE_FILE $TMP_FILE && sudo chmod 777 $TMP_FILE" && \
$SCP_CMD $HOST:$TMP_FILE $BACKUP_FILE
}

wait_for_ssh() {
local max_retries=30
local retry_interval=2

for ((i=1; i<=$max_retries; i++)); do
echo "Waiting for VM's SSH to become available... (Attempt $i/$max_retries)"
if $SSH_CMD -o ConnectTimeout=5 $HOST exit 2>/dev/null; then
echo "SSH connection established."
return 0
fi
sleep $retry_interval
done

echo "SSH connection could not be established after $max_retries attempts."
return 1
}

restore() {
if [ -f "$BACKUP_FILE" ]; then
echo "Restoring from backup..."
echo "Copying startup config file to the VM..."

$SCP_CMD $BACKUP_FILE $HOST:$TMP_FILE && $SSH_CMD $HOST "sudo cp $TMP_FILE $REMOTE_FILE && sudo config reload -y -f || true"
if wait_for_ssh; then
$SCP_CMD $BACKUP_FILE $HOST:$TMP_FILE && $SSH_CMD $HOST "sudo config load -y $TMP_FILE && sudo config save -y"
else
echo "Failed to establish SSH connection. Config copy operation aborted."
fi
else
echo "$BACKUP_FILE not found. Nothing to restore."
echo "$BACKUP_FILE not found. Nothing to push to the VM."
fi
}

Expand Down

0 comments on commit cbba711

Please sign in to comment.