Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
I
iot_manager_migration
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
CI / CD Analytics
Repository Analytics
Value Stream Analytics
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ajil.k
iot_manager_migration
Commits
bc798330
Commit
bc798330
authored
May 08, 2023
by
ajil.k
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added
parent
28acb8fc
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
116 additions
and
57 deletions
+116
-57
check_missing_docs.py
check_missing_docs.py
+54
-0
restoring_from_bson.py
restoring_from_bson.py
+2
-2
translator.py
translator.py
+60
-55
No files found.
check_missing_docs.py
0 → 100644
View file @
bc798330
import
pymongo
import
pandas
as
pd
# Connect to the MongoDB instance
client
=
pymongo
.
MongoClient
(
"mongodb://ilens:ilens
%401234
@192.168.0.220:4720/"
)
col_list
=
[
'mp201a'
,
'glens'
,
'ilens'
,
'ilens_ir4.0'
,
'dubai-munc'
,
'epda-rak'
,
'camw'
,
'ilens-avis'
,
'old_glens'
,
'old_calixto'
,
"ilens-mc"
]
dict_missing_docs
=
{}
mac
=
[]
device_id
=
[]
old_collection_name
=
[]
old_db
=
client
[
'Iot_manager(Test)'
]
for
collection_name
in
col_list
:
# Select the database and collection to compare
collection1
=
old_db
[
collection_name
]
new_db
=
client
[
"Assets_manager"
]
iot_devices_details_col
=
new_db
[
'Iot_devices_details'
]
# Retrieve all documents from the first collection
docs1
=
collection1
.
find
()
# Retrieve all documents from the second collection
docs2
=
iot_devices_details_col
.
find
()
# Create a set of all the values in the field to compare for the second collection
values2
=
set
([
doc
[
"mac_address"
]
for
doc
in
docs2
])
# Iterate through the first collection and check if each document exists in the second collection
missing_docs
=
[]
for
doc
in
docs1
:
if
doc
[
"mac"
]
not
in
values2
:
missing_docs
.
append
(
doc
)
# Print the missing documents
for
doc
in
missing_docs
:
mac
.
append
(
doc
[
"mac"
])
device_id
.
append
(
doc
[
"deviceId"
])
old_collection_name
.
append
(
collection_name
)
dict_missing_docs
[
"device_id"
]
=
device_id
dict_missing_docs
[
"mac"
]
=
mac
dict_missing_docs
[
"old_collection_name"
]
=
old_collection_name
print
(
dict_missing_docs
)
df
=
pd
.
DataFrame
(
dict_missing_docs
)
# Store the DataFrame as a CSV file
df
.
to_csv
(
'missing_devices.csv'
,
index
=
False
)
\ No newline at end of file
restoring_from_bson.py
View file @
bc798330
...
@@ -2,10 +2,10 @@ import bson
...
@@ -2,10 +2,10 @@ import bson
import
pymongo
import
pymongo
# Set up a MongoDB client
# Set up a MongoDB client
client
=
pymongo
.
MongoClient
(
'mongodb://
ilens:ilens
%401234
@192.168.0.220:4720/?authSource=admin
'
)
client
=
pymongo
.
MongoClient
(
'mongodb://
192.168.3.145:2717
'
)
# Select the database and collection to restore
# Select the database and collection to restore
db
=
client
[
"
Iot_manager(Test
)"
]
db
=
client
[
"
Assets_manager(old
)"
]
col_list
=
[
'mp201a'
,
'glens'
,
'ilens'
,
'ilens_ir4.0'
,
'dubai-munc'
,
'epda-rak'
,
'camw'
,
'ilens-avis'
,
'old_glens'
,
col_list
=
[
'mp201a'
,
'glens'
,
'ilens'
,
'ilens_ir4.0'
,
'dubai-munc'
,
'epda-rak'
,
'camw'
,
'ilens-avis'
,
'old_glens'
,
'old_calixto'
,
"ilens-mc"
]
'old_calixto'
,
"ilens-mc"
]
for
collection_name
in
col_list
:
for
collection_name
in
col_list
:
...
...
translator.py
View file @
bc798330
import
pymongo
import
pymongo
import
pytz
import
pytz
""" mongo dumping"""
import
os
import
os
import
subprocess
import
subprocess
import
zipfile
import
zipfile
...
@@ -9,57 +7,60 @@ from datetime import datetime, timedelta
...
@@ -9,57 +7,60 @@ from datetime import datetime, timedelta
from
schema
import
DeviceDetails
,
DeviceParameters
from
schema
import
DeviceDetails
,
DeviceParameters
# Set the MongoDB URL to connect to
#
mongo_url
=
'mongodb://ilens:ilens
%401234
@192.168.0.220:4720/?authSource=admin'
# """ mongo dumping"""
#
# Set the path to the mongodump executable
# # Set the MongoDB URL to connect to
mongodump_path
=
'C:/Program Files/MongoDB/Server/6.0/bin/mongodump.exe'
# mongo_url = 'mongodb://ilens:ilens%401234@192.168.0.220:4720/?authSource=admin'
#
# Set the name of the database to be dumped
# # Set the path to the mongodump executable
db_name
=
'Iot_manager(Test)'
# mongodump_path = 'C:/Program Files/MongoDB/Server/6.0/bin/mongodump.exe'
#
# Set the path where the dump will be saved
# # Set the name of the database to be dumped
dump_path
=
'E:/Iot_manager(Test)/dumped_data'
# db_name = 'Iot_manager(Test)'
#
# Build the command to be executed
# # Set the path where the dump will be saved
command
=
[
mongodump_path
,
'--uri'
,
mongo_url
,
'--db'
,
db_name
,
'--out'
,
dump_path
]
# dump_path = 'E:/Iot_manager(Test)/dumped_data'
#
# Execute the command
# # Build the command to be executed
subprocess
.
call
(
command
)
# command = [mongodump_path, '--uri', mongo_url, '--db', db_name, '--out', dump_path]
#
# mongo restore
# # Execute the command
# subprocess.call(command)
mongorestore_path
=
'C:/Program Files/MongoDB/Server/6.0/bin/mongorestore.exe'
#
now
=
datetime
.
now
()
# # mongo restore
#
date_str
=
now
.
strftime
(
"
%
Y-
%
m-
%
d"
)
# mongorestore_path = 'C:/Program Files/MongoDB/Server/6.0/bin/mongorestore.exe'
# now = datetime.now()
new_db_name
=
f
"Iot_manager(Test)_copy_{date_str}"
#
# date_str = now.strftime("%Y-%m-%d")
restore_command
=
[
mongorestore_path
,
'--uri'
,
mongo_url
,
'--db'
,
new_db_name
,
dump_path
+
"/"
+
db_name
]
#
subprocess
.
call
(
restore_command
)
# new_db_name = f"Iot_manager(Test)_copy_{date_str}"
#
# restore_command = [mongorestore_path, '--uri', mongo_url, '--db', new_db_name, dump_path + "/" + db_name]
def
zip_folder
(
folder_path
,
zip_path
):
# subprocess.call(restore_command)
with
zipfile
.
ZipFile
(
zip_path
,
'w'
,
compression
=
zipfile
.
ZIP_DEFLATED
)
as
zip
:
#
for
root
,
dirs
,
files
in
os
.
walk
(
folder_path
):
#
for
file
in
files
:
# def zip_folder(folder_path, zip_path):
file_path
=
os
.
path
.
join
(
root
,
file
)
# with zipfile.ZipFile(zip_path, 'w', compression=zipfile.ZIP_DEFLATED) as zip:
zip
.
write
(
file_path
,
file_path
[
len
(
folder_path
):])
# for root, dirs, files in os.walk(folder_path):
# for file in files:
# file_path = os.path.join(root, file)
zip_folder
(
dump_path
,
'E:/Iot_manager(Test)/dumped_data.zip'
)
# zip.write(file_path, file_path[len(folder_path):])
#
""" end of mongo dumping and backup"""
#
# zip_folder(dump_path, 'E:/Iot_manager(Test)/dumped_data.zip')
#
# """ end of mongo dumping and backup"""
""" mongo migration"""
""" mongo migration"""
# set up the connection to MongoDB
# set up the connection to MongoDB
client1
=
pymongo
.
MongoClient
(
"mongodb://
ilens:ilens
%401234
@192.168.0.220:4720/
"
)
client1
=
pymongo
.
MongoClient
(
"mongodb://
192.168.3.145:2717
"
)
col_list
=
[
'mp201a'
,
'glens'
,
'ilens'
,
'ilens_ir4.0'
,
'dubai-munc'
,
'epda-rak'
,
'camw'
,
'ilens-avis'
,
'old_glens'
,
col_list
=
[
'mp201a'
,
'glens'
,
'ilens'
,
'ilens_ir4.0'
,
'dubai-munc'
,
'epda-rak'
,
'camw'
,
'ilens-avis'
,
'old_glens'
,
'old_calixto'
,
"ilens-mc"
]
'old_calixto'
,
"ilens-mc"
]
db1
=
client1
[
"
Iot_manager(Test
)"
]
db1
=
client1
[
"
Assets_manager(old
)"
]
db2
=
client1
[
"Assets_manager"
]
db2
=
client1
[
"Assets_manager"
]
device_details
=
db2
[
"Iot_devices_details"
]
device_details
=
db2
[
"Iot_devices_details"
]
...
@@ -289,15 +290,19 @@ for collection_name in col_list:
...
@@ -289,15 +290,19 @@ for collection_name in col_list:
terminal
=
terminal
terminal
=
terminal
)
)
device_details
.
insert_one
(
device_data
.
dict
())
device_details_status
=
device_details
.
insert_one
(
device_data
.
dict
())
device_details
.
create_index
(
"software_name"
)
if
device_details_status
.
inserted_id
:
device_details
.
create_index
(
"device_id"
)
device_details
.
create_index
(
"software_name"
)
device_details
.
create_index
(
"mac_address"
)
device_details
.
create_index
(
"device_id"
)
device_details
.
create_index
(
"mac_address"
)
parameters_details
.
insert_one
(
parameter_data
.
dict
())
parameters_details
.
create_index
(
"mac_address"
)
parameters_details_status
=
parameters_details
.
insert_one
(
parameter_data
.
dict
())
parameters_details
.
create_index
(
"last_sync"
)
if
parameters_details_status
.
inserted_id
:
parameters_details
.
create_index
(
"software_name"
)
parameters_details
.
create_index
(
"mac_address"
)
parameters_details
.
create_index
(
"last_sync"
)
parameters_details
.
create_index
(
"software_name"
)
else
:
device_details
.
delete_one
({
"mac_address"
:
mac_address
})
print
(
f
"{collection_name} - {i}"
)
print
(
f
"{collection_name} - {i}"
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment