Transfer big databases between environments
Edit on GitHubSuppose you have two testing environments, and you need to transfer the data from one environment to another to perform different tests with the same data. If you have little data, you can export it by running the mysqldump
command locally. However, with large amounts of data, this method can be quite slow. To speed up the transfer, you can run the mysqldump
command on the Jenkins instance and upload the dump file to AWS S3. With this approach, data stays on the same network with the databases, which significantly speeds up the transfer.
To transfer data from one environment to another, take the following steps.
Prerequisites
Make sure the S3 bucket you are going to use for the transfer isn’t public, and only the people that need to access the data have access to it.
Export the data to an S3 bucket
-
Go to the Jenkins web interface of the environment you want to transfer the data from.
-
Go to General.
- Add the following commands to the job:
# update apk and install aws cli
apk update
apk add aws-cli
# confirm aws cli version
which aws
aws --version
# export the database to an S3 bucket
mysqldump --skip-lock-tables --host=$SPRYKER_DB_HOST --user=$SPRYKER_DB_ROOT_USERNAME --password=$SPRYKER_DB_ROOT_PASSWORD $SPRYKER_DB_DATABASE | gzip | aws s3 cp - s3://{S3_BUCKET_NAME}/backup.$(date +"%Y-%m-%d__%H-%M-%S").sql.gz
- Execute the job and wait for it to finish.
Running the job multiple times creates multiple dump files in the S3 bucket. To avoid cluttering the bucket with backup files, periodically clean up the bucket.
Import the data into the database
-
Go to the Jenkins web interface of the environment you want to transfer the data to.
-
Go to General.
-
Add the following commands to the job:
# update apk and install aws cli
apk update
apk add aws-cli
# confirm aws cli version
which aws
aws --version
# import the data from the s3 bucket to the target database
aws s3 cp s3://{S3_BUCKET_NAME}/{DUMP_FILE_NAME}.sql.gz - | zcat | mysql --host=$SPRYKER_DB_HOST --user=$SPRYKER_DB_ROOT_USERNAME --password=$SPRYKER_DB_ROOT_PASSWORD $SPRYKER_DB_DATABASE
- Execute the job and wait for it to finish.
Once the job finishes, the data should be in the target database.
Thank you!
For submitting the form