Reputation: 487
So I am at the place where I lost all of my Strapi data after moving from local to remote host and deploying my Strapi through herokuapp.com
I am using Strapi in my personal NEXT.JS project.
Luckily my Strapi database wasn't so huge and all content-types were kept so I was able to recreate the database quite quick. Also this was just a personal project.
I am wondering though, if I decide to push Strapi to use in in more professional context and in real project – how do I move from local development to deployment without loosing all data?
Is there a way to export everything before deployment and then import it into the deployed CMS or how does this work?
Also – what if I want to do it the other way around? I will keep working on my app using my Strapi on Heroku but at some point I will want to replicate the CMS locally. Where is the data stored and how do it get access to it?
Upvotes: 18
Views: 20759
Reputation: 7398
I use this script
set this ENV variables
/.env
BACKUP_AWS_ACCESS_KEY_ID="XXXX"
BACKUP_AWS_SECRET_ACCESS_KEY="XXXX"
BACKUP_AWS_REGION="us-east-1"
BACKUP_AWS_S3_BUCKET="XXXX-strapi-backup"
/backup_uploader.js
const AWS = require('aws-sdk');
const { execSync } = require('child_process');
const fs = require('fs');
// CONFIGS
const EXPORT_FILE_NAME = 'my-strapi-export';
const S3_BUCKET_NAME = process.env.BACKUP_AWS_S3_BUCKET;
// AWS CONFIG
AWS.config.update({
accessKeyId: process.env.BACKUP_AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.BACKUP_AWS_SECRET_ACCESS_KEY,
region: process.env.BACKUP_AWS_REGION
});
const s3 = new AWS.S3();
const uploadFileToS3 = () => {
const fileContent = fs.readFileSync(EXPORT_FILE_NAME);
// S3 FILENAME
const currentDate = new Date().toISOString().split('T')[0];
const s3Key = `${currentDate}.txt`;
const params = {
Bucket: S3_BUCKET_NAME,
Key: s3Key,
Body: fileContent
};
s3.upload(params, function(err, data) {
if (err) {
throw err;
}
console.log(`File uploaded successfully. ${data.Location}`);
});
};
// TIMEOUT
const TIMEOUT_MS = 60 * 60 * 1000; // 1h
const timeout = setTimeout(() => {
console.error('Process stopped after 1h.');
process.exit(1); // Esce con un errore
}, TIMEOUT_MS);
// RUN STRAPI EXPORT
console.log("Exporting data using Strapi...");
execSync(`yarn strapi export --no-encrypt --no-compress --file ${EXPORT_FILE_NAME}`, { stdio: 'inherit' });
// UPLOAD TO S3
console.log("Uploading exported file to S3...");
uploadFileToS3();
Upvotes: 0
Reputation: 81
You can do it via the CLI now, new from version 4.6. Strapi supports export, import, and transfer.
To create a tar of your data:
npm run strapi export --file my-strapi-export
To import data into your project:
npm run strapi import -f export_file.tar.gz
There are more options in the docs: https://docs.strapi.io/developer-docs/latest/developer-resources/data-management.html#export-data-using-the-cli-tool
With the new data export & import system, you can backup & restore your Strapi project data without acting manually and directly into the database.
More info: https://strapi.io/blog/announcing-strapi-v4.6
Upvotes: 7
Reputation: 765
Apparently they recently did a plugin tutorial that had to do with this issue. There is a plugin called strapi-plugin-import-export-content on git hub that might be able to help your issue.
Upvotes: 0
Reputation: 1077
Keep in mind Heroku is in the middle of shutting down its free tier, so using another provider like railway.app or render.com might be a good idea.
Anyway: As Eugene already mentioned in his answer there are 3 types of data that might have to transmitted (content types, the actual database, and files).
After your first deployment to Heroku you should end up with all content types being there, but with an empty database and no files.
Following this guide you will create an own database while setting up your project where you can now either export and import your database from your local development environment (which you would have to do manually) or put in new data by hand. Sometimes this is even better since development environments tend to include a lot of "Lorem Ipsum" content for testing purposes.
Future deployments should not reset your database though but keep your data from that environment.
Finally there are the files which I would recommend to store on Cloudinary since it's free, and Strapi offers an easy-to-use plugin for it. Just create a free account on Cloudinary, install the plugin in your Strapi project, and set your ENV variables for your production environment within Heroku.
Upvotes: 0
Reputation: 469
And there is no export/import content for now.
To export your data for example from the local environment to the production you have to handle:
app/public/uploads
I didn't tried this myself but it looks like a pretty tough task.
Conclusion: if it's OK for you to migrate only your content types, just put a git on your Strapi folder
Upvotes: 1