Fixed the script

This commit is contained in:
edwardro22
2017-09-12 15:24:27 +00:00
parent 8f4523fdd7
commit 2abe6b69c4
7 changed files with 91 additions and 184 deletions

View File

@@ -6,21 +6,6 @@
# ^ the script will "move" the collections if source and target server are the same
# but will "copy" (dump/restore) if source and target servers are different
from_host =os.environ["from_host"]
from_port =os.environ["from_port"]
from_auth_db =os.environ["from_auth_db"]
from_user =os.environ["from_user"]
from_pass =os.environ["from_pass"]
to_host =os.environ["to_host"]
to_port =os.environ["to_port"]
to_auth_db =os.environ["to_auth_db"]
to_user =os.environ["to_user"]
to_pass =os.environ["to_pass"]
ignore_empty_cols = True
# Do not migrate collections with 0 document count.
# Works only if from/to is on same host.
@@ -31,12 +16,26 @@ ignore_empty_cols = True
drop_target = False
dump_folder = 'dump'
import os
import sys
from pymongo import MongoClient
from urllib import quote_plus
from collections import OrderedDict
from_host =os.environ["cgr_from_host"]
from_port =os.environ["cgr_from_port"]
from_db =os.environ["cgr_from_db"]
from_auth_db =os.environ["cgr_from_auth_db"]
from_user =os.environ["cgr_from_user"]
from_pass =os.environ["cgr_from_pass"]
to_host =os.environ["cgr_to_host"]
to_port =os.environ["cgr_to_port"]
to_db =os.environ["cgr_to_db"]
to_auth_db =os.environ["cgr_to_auth_db"]
to_user =os.environ["cgr_to_user"]
to_pass =os.environ["cgr_to_pass"]
# same server
if from_host == to_host and from_port == to_port:
print('Migrating on same server...')

View File

@@ -7,17 +7,6 @@
# (https://github.com/antirez/redis/pull/2507)
# behaviour:
# ^ the script will not overwrite keys on the destination server/database
from_host = '192.168.100.40'
from_port = 6379
from_db = 11
from_pass = ''
to_host = '192.168.100.40'
to_port = 6379
to_db = 10
to_pass = '' # Not used
keymask = '*'
timeout = 2000
@@ -25,17 +14,15 @@ import time
import redis
import os
from_host =os.environ["from_host"]
from_port =os.environ["from_port"]
from_db =os.environ["from_db"]
from_pass =os.environ["from_pass"]
to_host =os.environ["to_host"]
to_port =os.environ["to_port"]
to_db =os.environ["to_db"]
to_pass =os.environ["to_pass"]
from_host =str(os.environ["cgr_from_host"])
from_port = int(os.environ["cgr_from_port"])
from_db =int(os.environ["cgr_from_db"])
from_pass =os.environ["cgr_from_pass"]
to_host =os.environ["cgr_to_host"]
to_port =int(os.environ["cgr_to_port"])
to_db =int(os.environ["cgr_to_db"])
# to_pass =os.environ["cgr_to_pass"] # Not used
from_redis = redis.Redis(host = from_host, port = from_port, password=from_pass, db = from_db)
to_redis = redis.Redis(host = to_host, port = to_port, db = to_db)

View File

@@ -55,4 +55,9 @@ END //
DELIMITER ;
CALL cgrates.migration();
CALL cgrates.migration();
ALTER TABLE cdrs CHANGE COLUMN `usage` `usage_old` DECIMAL(30,9);
ALTER TABLE cdrs ADD `usage` DECIMAL(30);
UPDATE cdrs SET `usage` = `usage_old` * 1000000000 WHERE usage_old IS NOT NULL;
ALTER TABLEX cdrs DROP COLUMN usage_old;

View File

@@ -41,3 +41,7 @@ BEGIN
END LOOP;
END
$$;
ALTER TABLE cdrs RENAME COLUMN usage to usage_old;
ALTER TABLE cdrs ADD usage NUMERIC(30);
UPDATE cdrs SET usage = usage_old * 1000000000 WHERE usage_old IS NOT NULL;
ALTER TABLE cdrs DROP COLUMN usage_old;

View File

@@ -1,95 +1,89 @@
#! /usr/bin/env sh
#settings
echo ""
echo "rc7_to_rc8.sh"
#settings
#DBs Config
datadb="redis"
stordb="mysql"
echo "dataDB:"$datadb " storDB:"$stordb
echo ""
$datadb="redis"
if [$datadb="redis"];then
#dataDBs
case $datadb in
"redis")
#Redis Config
export from_host = '192.168.100.40'
export from_port = 6379
export from_db = 11
export from_pass = ''
export cgr_from_host='127.0.0.1'
export cgr_from_port=6379
export cgr_from_db=11
export cgr_from_pass=''
export to_host = '192.168.100.40'
export to_port = 6379
export to_db = 10
export to_pass = '' # Not used
else if [$datadb="mongo"];then
export cgr_to_host='127.0.0.1'
export cgr_to_port=6379
export cgr_to_db=10
export cgr_to_pass='' # Not used
;;
"mongo")
#Mongo Config
export from_host = '127.0.0.1'
export from_port = '27017'
export from_db = '11'
export from_auth_db = 'cgrates' # Auth db on source server
export from_user = 'cgrates'
export from_pass = ''
export cgr_from_host='127.0.0.1'
export cgr_from_port='27017'
export cgr_from_db='11'
export cgr_from_auth_db='cgrates' # Auth db on source server
export cgr_from_user='cgrates'
export cgr_from_pass=''
export to_host = '127.0.0.1'
export to_port = '27017'
export to_db = '10'
export to_auth_db = "cgrates" # Auth db on target server
export to_user = 'cgrates'
export to_pass = ''
fi
export cgr_to_host='127.0.0.1'
export cgr_to_port='27017'
export cgr_to_db='10'
export cgr_to_auth_db="cgrates" # Auth db on target server
export cgr_to_user='cgrates'
export cgr_to_pass=''
;;
esac
export PGPASSWORD="CGRateS.org"
#StorDBs
case $stordb in
"mysql")
#mysql Config
user="cgrates"
host="127.0.0.1"
db="cgrates"
;;
if [$stordb="redis"];then
#Redis Config
export from_host = '192.168.100.40'
export from_port = 6379
export from_db = 11
export from_pass = ''
export to_host = '192.168.100.40'
export to_port = 6379
export to_db = 10
export to_pass = '' # Not used
else if [$stordb="mysql"];then
#Mongo Config
export from_host = '127.0.0.1'
export from_port = '27017'
export from_db = '11'
export from_auth_db = 'cgrates' # Auth db on source server
export from_user = 'cgrates'
export from_pass = ''
export to_host = '127.0.0.1'
export to_port = '27017'
export to_db = '10'
export to_auth_db = "cgrates" # Auth db on target server
export to_user = 'cgrates'
export to_pass = ''
fi
"postgres")
#postgres Config
user="cgrates"
host="127.0.0.1"
db="cgrates"
;;
esac
DIR="$(dirname "$(readlink -f "$0")")"
#DataDB switch
case $datadb in
"redis")
./dbsmerge_redis.py
;;
"mongo")
./dbsmerge_mongo.py
;;
esac
#StorDB switch
case $stordb in
"mysql")
"mysql")
mysql -u$user -p$PGPASSWORD -h $host < "$DIR"/mysql_tables_update.sql
up=$?
mysql -u$user -p$PGPASSWORD -h $host -D cgrates < "$DIR"/mysql_cdr_migration.sql
mig=$?
;;
"postgres")
psql -U $user -h $host -d cgrates -f "$DIR"/pq_tables_update.sql
up=$?
@@ -99,8 +93,6 @@ mig=$?
esac
if [ $up = 0 ] && [ $mig = 0 ]; then
echo -e "\n\t+++ CGR-DB successfully set-up! +++\n"
echo -e "\n\t+++ The script ran successfully ! +++\n"
exit 0
fi
fi

View File

@@ -1,40 +0,0 @@
#!/usr/bin/python
# depends:
# ^ mysql (debian: python-mysql.connector)
host = '192.168.100.40'
port = 3306
database = 'cgrates'
user = 'root'
password = 'CGRateS.org'
import mysql.connector
config = {
'user': user,
'password': password,
'host': host,
'port': port,
'database': database,
}
print('Connecting to MySQL...')
cnx = mysql.connector.connect(**config)
cursor = cnx.cursor()
print('Renaming old column...')
cursor.execute('ALTER TABLE cdrs CHANGE COLUMN `usage` `usage_old` DECIMAL(30,9)')
print('Adding new column...')
cursor.execute('ALTER TABLE cdrs ADD `usage` DECIMAL(30)')
print('Setting new values...')
cursor.execute('UPDATE cdrs SET `usage` = `usage_old` * 1000000000 WHERE usage_old IS NOT NULL')
print('Deleting old column...')
cursor.execute('ALTER TABLEX cdrs DROP COLUMN usage_old')
print('Closing MySQL connection...')
cnx.close()

View File

@@ -1,40 +0,0 @@
#!/usr/bin/python
# depends:
# ^ psycopg2 (debian: python-psycopg2)
host = '127.0.0.1'
port = 5432
database = 'cgrates'
user = 'cgrates'
password = 'CGRateS.org'
import psycopg2
print('Connecting to PostgreSQL...')
cnx = psycopg2.connect(
host=host,
port=port,
dbname=database,
user=user,
password=password
)
cursor = cnx.cursor()
print('Renaming old column...')
cursor.execute('ALTER TABLE cdrs RENAME COLUMN usage to usage_old')
print('Adding new column...')
cursor.execute('ALTER TABLE cdrs ADD usage NUMERIC(30)')
print('Setting new values...')
cursor.execute('UPDATE cdrs SET usage = usage_old * 1000000000 WHERE usage_old IS NOT NULL')
print('Deleting old column...')
cursor.execute('ALTER TABLE cdrs DROP COLUMN usage_old')
print('Commiting...')
cnx.commit()
print('Closing PostgreSQL connection...')
cnx.close()