Skip to content

Instantly share code, notes, and snippets.

@juan131
Last active September 19, 2019 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juan131/51b91abfb525ffc9a7e70a75767dbc7f to your computer and use it in GitHub Desktop.
Save juan131/51b91abfb525ffc9a7e70a75767dbc7f to your computer and use it in GitHub Desktop.
Upgrade Bitnami Redash Script

Usage

  • Edit the "directory variables" BACKUPS_DIR, INSTALLDIR with the path to you stack installation and a backup directory.
  • Edit the variable PASSWORD with your PostgreSQL password.
  • Edit the variables PREVIOUS_VERSION & FINAL_VERSION with the values of you current Redash version and the version you want to migraton to, respectively.

| NOTE: Every variable can be set using Environment Variables.

Examples:

1 - Simple execution:

sudo ./upgrade-redash.sh

2 - Using Environment Variables:

sudo INSTALLDIR="/opt/bitnami" PASSWORD="My-Password" ./upgrade-redash.sh
#!/bin/bash -e
# Check GIST Readme.md file for more information.
# Directory Variables
BACKUPS_DIR=${BACKUPS_DIR:-"/path/to/your/backup/directory"}
INSTALLDIR=${INSTALLDIR:-"/path/to/your/stack/installation/directory"}
PASSWORD=${PASSWORD:-""} # PostgreSQL password
# Versions
PREVIOUS_VERSION=${PREVIOUS_VERSION:-"0.11.1.2095-1"}
FINAL_VERSION=${FINAL_VERSION:-"3.0.0-0"}
function get_next_version() {
declare -a VERSIONS=('0.11.1.2095-1' '0.12.0.2449-1' '1.0.3-1' '2.0.0-0' '2.0.1-0' '3.0.0-0')
NEXT_VERSION="$(echo "${VERSIONS[@]}" | awk -F "$1" '{print $2}' | awk '{print $1}')"
echo "$NEXT_VERSION"
}
# Silent execution wrapper
function quiet() {
if [ -n "$VERBOSE" ];then
"$@" >/dev/null 2>&1
else
"$@"
fi
}
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
mkdir -p "$BACKUPS_DIR"
CURRENT_VERSION=$PREVIOUS_VERSION
while [ "$FINAL_VERSION" != "$CURRENT_VERSION" ]; do
NEXT_VERSION=$(get_next_version "$CURRENT_VERSION")
if [ -z "$NEXT_VERSION" ]; then
echo "Err: There are no new versions"
else
echo -e "Backing up previous installation..."
# Backup the PostgreSQL database
PGPASSWORD="$PASSWORD" "$INSTALLDIR"/postgresql/bin/pg_dump -U postgres bitnami_redash > "$BACKUPS_DIR"/postgresql-backup-"$CURRENT_VERSION".sql
quiet "$INSTALLDIR"/ctlscript.sh stop
# Run migration scripts
# Backup Redash stack
tar -czf "$BACKUPS_DIR/stack-backup-$CURRENT_VERSION.tar.gz" -C "$INSTALLDIR" .
rm -rf "$INSTALLDIR"
# Download next installer version
INSTALLER="bitnami-redash-$NEXT_VERSION-linux-x64-installer.run"
INSTALLER_URL="https://downloads.bitnami.com/files/stacks/redash/$NEXT_VERSION/$INSTALLER"
wget -q "$INSTALLER_URL"
# Install new Redash
echo -e "Installing Redash stack $NEXT_VERSION. It could take some minutes..."
chmod +x "$INSTALLER"
./"$INSTALLER" --mode unattended --base_password "$PASSWORD" --base_mail "user@example.com" --prefix "$INSTALLDIR"
# Import database backup
quiet "$INSTALLDIR"/ctlscript.sh stop && quiet "$INSTALLDIR"/ctlscript.sh start postgresql
quiet PGPASSWORD="$PASSWORD" "$INSTALLDIR"/postgresql/bin/dropdb -U postgres bitnami_redash
quiet PGPASSWORD="$PASSWORD" "$INSTALLDIR"/postgresql/bin/createdb -U postgres bitnami_redash
quiet PGPASSWORD="$PASSWORD" "$INSTALLDIR"/postgresql/bin/psql -U postgres -c "GRANT ALL PRIVILEGES ON DATABASE bitnami_redash TO bn_redash;"
quiet PGPASSWORD="$PASSWORD" "$INSTALLDIR"/postgresql/bin/psql -U postgres -c "ALTER DATABASE bitnami_redash OWNER TO bn_redash;"
quiet PGPASSWORD="$PASSWORD" "$INSTALLDIR"/postgresql/bin/psql -U postgres bitnami_redash < "$BACKUPS_DIR"/postgresql-backup-"$CURRENT_VERSION".sql
quiet "$INSTALLDIR"/ctlscript.sh start redis > /dev/null 2>&1
echo -e "Migrating previous Redash installation..."
# Run migration scripts
cd "$INSTALLDIR"/apps/redash/htdocs/ || exit
# shellcheck source=/dev/null
. "$INSTALLDIR"/scripts/setenv.sh
# shellcheck source=/dev/null
. "$INSTALLDIR"/apps/redash/htdocs/venv/bin/activate
if [ "$(echo "$NEXT_VERSION" | awk -F '.' '{print $1}')" -eq 0 ]; then
for P in $INSTALLDIR/apps/redash/htdocs/migrations/*; do
quiet BITNAMI_ROOT="$INSTALLDIR" "$INSTALLDIR"/apps/redash/htdocs/bin/run python "$P"
done
else
quiet BITNAMI_ROOT="$INSTALLDIR" bin/run ./manage.py db upgrade
fi
cd - > /dev/null || exit
deactivate
# Check that everything works as expected on the UI & logs
quiet "$INSTALLDIR"/ctlscript.sh start
echo -e "Please check everything works as expected on Redash UI... \\n\\n"
sleep 60 # Time to check everything is fine
CURRENT_VERSION="$NEXT_VERSION"
fi
done
Copy link

ghost commented Sep 19, 2019

While using different versions for previous and final version, getting this error after run the script:
Err: there are no new versions

PREVIOUS_VERSION=${PREVIOUS_VERSION:-"5.0.1-0"}
FINAL_VERSION=${FINAL_VERSION:-"5.0.2-0"}

function get_next_version() {
declare -a VERSIONS=('5.0.2-0')
NEXT_VERSION="$(echo "${VERSIONS[@]}" | awk -F "$1" '{print $2}' | awk '{print $1}')"
echo "$NEXT_VERSION"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment