From afa1e19c8336c967b89fbbb8d1dde9ecec8d466d Mon Sep 17 00:00:00 2001 From: wh1te909 Date: Wed, 24 Feb 2021 20:39:02 +0000 Subject: [PATCH] also grep postgres info during restore #285 --- docs/docs/restore.md | 24 +----------------------- restore.sh | 43 ++++++++++++++++++------------------------- 2 files changed, 19 insertions(+), 48 deletions(-) diff --git a/docs/docs/restore.md b/docs/docs/restore.md index 95f82920..188486e0 100644 --- a/docs/docs/restore.md +++ b/docs/docs/restore.md @@ -25,29 +25,7 @@ Download the restore script. wget https://raw.githubusercontent.com/wh1te909/tacticalrmm/master/restore.sh ``` -Edit `restore.sh` with your text editor of choice. - -Change the postgres username/password at the top of the file. -You can find this info in the following file: -``` -/rmm/api/tacticalrmm/tacticalrmm/local_settings.py -``` - -Look for this section and grab the USER / PASSWORD: -```python -DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.postgresql', - 'NAME': 'tacticalrmm', - 'USER': 'someusername', - 'PASSWORD': 'somepassword', - 'HOST': 'localhost', - 'PORT': '5432', - } -} -``` - -Copy the [backup](backup.md) tar file to the new server and then call the restore script, passing it the backup file as the first argument: +Call the restore script, passing it the backup file as the first argument: ```bash chmod +x restore.sh diff --git a/restore.sh b/restore.sh index bcaa61bf..8784b60e 100755 --- a/restore.sh +++ b/restore.sh @@ -1,15 +1,9 @@ #!/bin/bash -##################################################### - -pgusername="changeme" -pgpw="hunter2" - -##################################################### - -SCRIPT_VERSION="18" +SCRIPT_VERSION="19" SCRIPT_URL='https://raw.githubusercontent.com/wh1te909/tacticalrmm/master/restore.sh' +sudo apt update sudo apt install -y curl wget dirmngr gnupg lsb-release GREEN='\033[0;32m' @@ -31,12 +25,6 @@ fi rm -f $TMP_FILE -if [[ "$pgusername" == "changeme" || "$pgpw" == "hunter2" ]]; then - printf >&2 "${RED}You must change the postgres username/password at the top of this file.${NC}\n" - printf >&2 "${RED}Check the github readme for where to find them.${NC}\n" - exit 1 -fi - osname=$(lsb_release -si); osname=${osname^} osname=$(echo "$osname" | tr '[A-Z]' '[a-z]') fullrel=$(lsb_release -sd) @@ -218,18 +206,7 @@ sudo apt update sudo apt install -y postgresql-13 sleep 2 -print_green 'Restoring the database' -sudo -u postgres psql -c "DROP DATABASE IF EXISTS tacticalrmm" -sudo -u postgres psql -c "CREATE DATABASE tacticalrmm" -sudo -u postgres psql -c "CREATE USER ${pgusername} WITH PASSWORD '${pgpw}'" -sudo -u postgres psql -c "ALTER ROLE ${pgusername} SET client_encoding TO 'utf8'" -sudo -u postgres psql -c "ALTER ROLE ${pgusername} SET default_transaction_isolation TO 'read committed'" -sudo -u postgres psql -c "ALTER ROLE ${pgusername} SET timezone TO 'UTC'" -sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE tacticalrmm TO ${pgusername}" - -gzip -d $tmp_dir/postgres/*.psql.gz -PGPASSWORD=${pgpw} psql -h localhost -U ${pgusername} -d tacticalrmm -f $tmp_dir/postgres/db*.psql print_green 'Restoring MongoDB' @@ -281,6 +258,22 @@ sudo cp /rmm/natsapi/bin/nats-api /usr/local/bin sudo chown ${USER}:${USER} /usr/local/bin/nats-api sudo chmod +x /usr/local/bin/nats-api +print_green 'Restoring the database' + +pgusername=$(grep -w USER /rmm/api/tacticalrmm/tacticalrmm/local_settings.py | sed 's/^.*: //' | sed 's/.//' | sed -r 's/.{2}$//') +pgpw=$(grep -w PASSWORD /rmm/api/tacticalrmm/tacticalrmm/local_settings.py | sed 's/^.*: //' | sed 's/.//' | sed -r 's/.{2}$//') + +sudo -u postgres psql -c "DROP DATABASE IF EXISTS tacticalrmm" +sudo -u postgres psql -c "CREATE DATABASE tacticalrmm" +sudo -u postgres psql -c "CREATE USER ${pgusername} WITH PASSWORD '${pgpw}'" +sudo -u postgres psql -c "ALTER ROLE ${pgusername} SET client_encoding TO 'utf8'" +sudo -u postgres psql -c "ALTER ROLE ${pgusername} SET default_transaction_isolation TO 'read committed'" +sudo -u postgres psql -c "ALTER ROLE ${pgusername} SET timezone TO 'UTC'" +sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE tacticalrmm TO ${pgusername}" + +gzip -d $tmp_dir/postgres/*.psql.gz +PGPASSWORD=${pgpw} psql -h localhost -U ${pgusername} -d tacticalrmm -f $tmp_dir/postgres/db*.psql + cd /rmm/api python3.9 -m venv env source /rmm/api/env/bin/activate