1. An overview
As mentioned in the first article in this series, as of now the latest version of Strapi does not support automatic migration data, so we need to manually migration data to the target machine.
So what is Migration?
In simple terms, it is to migrate the existing entire website data from one server to another.
Suppose, according to our previous introduction, we have setup a set of Strapi app on ServerA, and then we have carried out a series of development on ServerA, and then due to the change of some requirements or the change of the deployment environment, We need to port the entire existing Strapi system to ServerB, a process called Migration.
Existing schemes based on MongoDB
Since Strapi doesn’t offer a mature solution right now, you have to do it yourself. As described in previous articles, the Strapi App consists of two parts: Stapi website and mongoDB (we used mongoDB before), so our solution is migrate data of these two parts.
Note: This article is written on the assumption that you are using a Linux operating system, and you are best off following our previous articles in this series to build the entire Strapi system.
If you have any questions, please contact me directly. I am willing to help you solve the problems you encounter in the practical application process.
2. Prerequisites
- MongoDB Database Tools
- Mongodump – Used to dump the entire database
- Mongorestore — Data used to restore the entire database
3. Dump data on the original server
3.1 Run the following code to dump the entire MongoDB
mongodump --uri="mongodb://localhost:27017" -u=strapi -p=strapi --authenticationDatabase=admin -d=strapi -o="./dump"
Copy the code
3.2 Packing dumped MongoDB Data
tar -czvf dump.tar.gz ./dump
Copy the code
3.3 Back up necessary data of Strapi website
- delete
node-modules
folder# go to the Strapi website folder. In my system, this folder is strapistrapi-app cd strapistrapi-app rm -fr node_modules cd.Copy the code
- Package the entire site
# strapistrapi-app is the folder where the Strapi website is located tar -czvf strapi.tar.gz ./strapistrapi-app Copy the code
4. Restore data on the target machine
4.1 Start Strapi with Docker-compose
You can follow the previous articles and start the entire StrAPI with docker-compose, or get the repo with Git and execute start.sh
4.2 Suspending the Strapi Container
You need to run Docker ps first to see the ID of the Strapi container
docker stop <strapi container id>
Copy the code
4.3 Deleting the StrAPI database from Mongo Container
You will need to run Docker ps first to see the ID of the Mongo container
docker exec -it <mongo container id>
The docker-compose file specifies that the default mongoDB username and password are strapi
mongo -u strapi -p strapi
use strapi
db.dropDatabase()
Copy the code
4.4 the Copystrapi.tar.gz
& dump.tar.gz
To target machine
Strapi.tar. gz & dump.tar.gz is the dump data for strapi sites and mongoDB generated in 3.3.
4.5 Decompress the preceding two files
# extract strapi
tar -xzvf strapi.tar.gz
# extract mongo
tar -xzvf dump.tar.gz
Copy the code
4.6 Running the restore command
If you clone the repO mentioned earlier, you can directly execute the restore file in it
./restore.sh
Otherwise, you need to run the following command
mongorestore --uri="mongodb://localhost:27017" -u=strapi -p=strapi --authenticationDatabase=admin dump/
Copy the code
4.7 Restart the entire Strapi site
If you clone the repO mentioned earlier, you can simply execute the start file in it
./start.sh
Otherwise, you need to run the following command
docker-compose up -d
Copy the code