From 1211e8db9f5e7a0248cfd08437a053425acca4a8 Mon Sep 17 00:00:00 2001 From: David Randall Date: Thu, 6 Jan 2022 18:25:47 -0500 Subject: [PATCH 1/3] Migrate meshcentral to postgres from mongodb --- scripts/migrate-mesh-to-postgres.sh | 50 +++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100755 scripts/migrate-mesh-to-postgres.sh diff --git a/scripts/migrate-mesh-to-postgres.sh b/scripts/migrate-mesh-to-postgres.sh new file mode 100755 index 0000000..77bd403 --- /dev/null +++ b/scripts/migrate-mesh-to-postgres.sh @@ -0,0 +1,50 @@ +#!/usr/bin/env bash + +print_green 'Creating login for the meshcentral database' +meshdbuser=$(cat /dev/urandom | tr -dc 'a-z' | fold -w 8 | head -n 1) +meshdbpw=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 20 | head -n 1) + +print_green 'Creating database for the meshcentral' +sudo -u postgres psql -c "CREATE DATABASE meshcentral" +sudo -u postgres psql -c "CREATE USER ${meshdbuser} WITH PASSWORD '${meshdbpw}'" +sudo -u postgres psql -c "ALTER ROLE ${meshdbuser} SET client_encoding TO 'utf8'" +sudo -u postgres psql -c "ALTER ROLE ${meshdbuser} SET default_transaction_isolation TO 'read committed'" +sudo -u postgres psql -c "ALTER ROLE ${meshdbuser} SET timezone TO 'UTC'" +sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE meshcentral TO ${meshdbuser}" + +print_green 'Export meshcentral database' +cd /meshcentral +node node_modules/meshcentral --dbexport + +# Database cannot be specified +# https://github.com/Ylianst/MeshCentral/issues/3398 +MESH_PG_DB="" +MESH_PG_USER="$meshdbuser" +MESH_PG_PW="$meshdbpw" +MESH_PG_PORT="5432" +MESH_PG_HOST="localhost" + +cat /meshcentral/meshcentral-data/config.json | + jq " del(.settings.MongoDb, .settings.MongoDbName) " | + jq " .settings.postgres.user |= \"${MESH_PG_USER}\" " | + jq " .settings.postgres.password |= \"${MESH_PG_PW}\" " | + jq " .settings.postgres.port |= \"${MESH_PG_PORT}\" " | + jq " .settings.postgres.host |= \"${MESH_PG_HOST}\" " > /meshcentral/meshcentral-data/config-postgres.json + +# Backup Meshcentral config for MongoDB +print_green 'Backing up meshcentral config' +cp /meshcentral/meshcentral-data/config.json /meshcentral/meshcentral-data/config-mongodb-$(date "+%Y%m%dT%H%M%S").json +cp /meshcentral/meshcentral-data/config-postgres.json /meshcentral/meshcentral-data/config.json + +print_green 'Restart meshcentral' +sudo systemctl restart meshcentral +print_green 'Import Database from meshcentral' +node node_modules/meshcentral --dbimport +print_green 'Final restart of meshcentral' +sudo systemctl restart meshcentral + +print_green 'Shutting down MongoDB' +sudo systemctl stop mongod.service +print_green 'Disabling MongoDB' +sudo systemctl disable mongod.service + From f2e6d7c2be709265ec81ff6c0be5682919ca49cf Mon Sep 17 00:00:00 2001 From: David Randall Date: Thu, 6 Jan 2022 20:01:54 -0500 Subject: [PATCH 2/3] Make meshcentral install location adjustable --- scripts/migrate-mesh-to-postgres.sh | 66 ++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 19 deletions(-) diff --git a/scripts/migrate-mesh-to-postgres.sh b/scripts/migrate-mesh-to-postgres.sh index 77bd403..e0fc48a 100755 --- a/scripts/migrate-mesh-to-postgres.sh +++ b/scripts/migrate-mesh-to-postgres.sh @@ -1,45 +1,73 @@ #!/usr/bin/env bash +# NOTE: Node might need some postgres modules installed. +# cd $mesh_install +# node npm install pg pgtools + +# NOTE: These can be modified if necessary. +mesh_install="/meshcentral" +mesh_data="/meshcentral/meshcentral-data" +mesh_program="node_modules/meshcentral" + +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +RED='\033[0;31m' +NC='\033[0m' + +print_green() { + printf >&2 "${GREEN}%0.s-${NC}" {1..80} + printf >&2 "\n" + printf >&2 "${GREEN}${1}${NC}\n" + printf >&2 "${GREEN}%0.s-${NC}" {1..80} + printf >&2 "\n" +} + print_green 'Creating login for the meshcentral database' meshdbuser=$(cat /dev/urandom | tr -dc 'a-z' | fold -w 8 | head -n 1) meshdbpw=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 20 | head -n 1) -print_green 'Creating database for the meshcentral' -sudo -u postgres psql -c "CREATE DATABASE meshcentral" -sudo -u postgres psql -c "CREATE USER ${meshdbuser} WITH PASSWORD '${meshdbpw}'" -sudo -u postgres psql -c "ALTER ROLE ${meshdbuser} SET client_encoding TO 'utf8'" -sudo -u postgres psql -c "ALTER ROLE ${meshdbuser} SET default_transaction_isolation TO 'read committed'" -sudo -u postgres psql -c "ALTER ROLE ${meshdbuser} SET timezone TO 'UTC'" -sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE meshcentral TO ${meshdbuser}" - -print_green 'Export meshcentral database' -cd /meshcentral -node node_modules/meshcentral --dbexport - -# Database cannot be specified +# Postgres database name has to be "meshcentral" # https://github.com/Ylianst/MeshCentral/issues/3398 -MESH_PG_DB="" +meshdbname="meshcentral" + +# Meshcentral configs +MESH_PG_DB="$meshdbname" MESH_PG_USER="$meshdbuser" MESH_PG_PW="$meshdbpw" MESH_PG_PORT="5432" MESH_PG_HOST="localhost" -cat /meshcentral/meshcentral-data/config.json | +print_green 'Creating postgres database for the meshcentral' +sudo -u postgres psql < /meshcentral/meshcentral-data/config-postgres.json + jq " .settings.postgres.host |= \"${MESH_PG_HOST}\" " > "${mesh_data}/config-postgres.json" # Backup Meshcentral config for MongoDB print_green 'Backing up meshcentral config' -cp /meshcentral/meshcentral-data/config.json /meshcentral/meshcentral-data/config-mongodb-$(date "+%Y%m%dT%H%M%S").json -cp /meshcentral/meshcentral-data/config-postgres.json /meshcentral/meshcentral-data/config.json +cp "${mesh_data}/config.json" "${mesh_data}/config-mongodb-$(date "+%Y%m%dT%H%M%S").json" +cp "${mesh_data}/config-postgres.json" "${mesh_data}/config.json" print_green 'Restart meshcentral' sudo systemctl restart meshcentral print_green 'Import Database from meshcentral' -node node_modules/meshcentral --dbimport +node "${mesh_program}" --dbimport print_green 'Final restart of meshcentral' sudo systemctl restart meshcentral From 5bbc1d191564156979a761b850b75de64916b7a0 Mon Sep 17 00:00:00 2001 From: David Randall Date: Thu, 6 Jan 2022 20:11:11 -0500 Subject: [PATCH 3/3] Check if jq is installed --- scripts/migrate-mesh-to-postgres.sh | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/scripts/migrate-mesh-to-postgres.sh b/scripts/migrate-mesh-to-postgres.sh index e0fc48a..33a81d6 100755 --- a/scripts/migrate-mesh-to-postgres.sh +++ b/scripts/migrate-mesh-to-postgres.sh @@ -9,6 +9,14 @@ mesh_install="/meshcentral" mesh_data="/meshcentral/meshcentral-data" mesh_program="node_modules/meshcentral" +if ! which jq >/dev/null +then + echo "jq is not installed" + echo "Please install jq with:" + echo " sudo apt-get install jq" + exit 1 +fi + GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m'