Skip to content

Instantly share code, notes, and snippets.

@nvn-odoo
Forked from olilau/odoo_download_with_sftp.sh
Created April 24, 2020 17:47
Show Gist options
  • Save nvn-odoo/723c63d321ec5a78c84e8668b9901226 to your computer and use it in GitHub Desktop.
Save nvn-odoo/723c63d321ec5a78c84e8668b9901226 to your computer and use it in GitHub Desktop.
Download an upgraded Odoo database using SFTP.
#!/bin/bash
set -e
command -v curl >/dev/null 2>&1 || { echo >&2 "I require curl but it's not installed. Aborting."; exit 1; }
command -v jq >/dev/null 2>&1 || { echo >&2 "I require jq but it's not installed. Aborting."; exit 1; }
# REQUIRED PARAMETERS:
REQUEST_ID="17654"
KEY="0zWIetJPo7kIWSxVWKUDTQ=="
SSH_KEYS=/path/to/your/authorized_keys
# END of REQUIRED PARAMETERS
MAX_SFTP_TRIES=32
BASE_URL="https://upgrade.odoo.com"
# check public key file is valid:
ssh-keygen -l -f $SSH_KEYS 1>/dev/null
# Requesting an SFTP access:
REQUEST_SFTP_ACCESS_URL="${BASE_URL}/database/v1/request_sftp_access"
URL_PARAMS="key=${KEY}&request=${REQUEST_ID}"
curl -sS "${REQUEST_SFTP_ACCESS_URL}?${URL_PARAMS}" -F ssh_keys=@${SSH_KEYS} > request_sftp_result.json
# check for failures
failures=$(cat request_sftp_result.json | jq -r '.failures[]')
if [ "$failures" != "" ]; then
echo $failures | jq -r '.'
exit 3
else
echo "request_sftp_access: ok"
fi
#SSH_OPTIONS="-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
SFTP_PORT=$(cat request_sftp_result.json | jq -r '.request.sftp_port')
SFTP_USER=$(cat request_sftp_result.json | jq -r '.request.sftp_user')
SFTP_SERVER=$(cat request_sftp_result.json | jq -r '.request.hostname')
SFTP_FILENAME=$(cat request_sftp_result.json | jq -r '.request.shared_file')
SFTP_CMD="sftp -b batch.sftp -P ${SFTP_PORT} ${SSH_OPTIONS} ${SFTP_USER}@${SFTP_SERVER}"
echo "get -a \"$SFTP_FILENAME\"" > batch.sftp
echo "bye" >> batch.sftp
counter=$MAX_SFTP_TRIES
until $SFTP_CMD
do
if [ "$counter" -le "0" ]; then
echo "Tried too many times. Exiting"
exit 4
fi
echo "trying again (# of tries left: $counter)";
sleep 3
counter=$[$counter-1];
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment