[TomatoCart]Mysql database backup and migration

For an online store, it is important to backup your database daily. Otherwise, you may lose important data such as customer orders, client information and so on. In this article I will show you how to backup database manually and automatically. In addition, I will also show you how to perform a database migration from previous backup data.

Backup specific database manually

Step 1. Login into security shell of your web server with ssh client.

ssh username@ipaddress

Step 2. Run following command to backup specific database:

mysqldump -hlocalhost -uroot -proot tomatocart | gzip > /var/backups/mysql/20160123/tomatocart.gz

As you can see, i backup the tomatocart database into /var/backups/mysql/20160123/tomatocart.gz with username root and password root. Before saving it, the database will be compressed with gzip.

If you are not logged into server as a root user, you need to execute sudo mysqldump.

Backup all of databases manually

Step 1. Login into security shell of your web server with ssh client.

Step 2. Create a shell script named backup.sh under ~/bin directory:

# Simple script to backup MySQL databases

# Parent backup directory

# MySQL settings

# Create backup directory and set permissions
backup_date=`date +%Y_%m_%d`

mkdir -p "${backup_dir}"
chmod 700 "${backup_dir}"

# Get MySQL databases
mysql_databases=`echo 'show databases' | mysql --user=${mysql_user} --password=${mysql_password} -B | sed /^Database$/d`

# Backup and compress each database
for database in $mysql_databases
  if [ "${database}" == "information_schema" ] || [ "${database}" == "performance_schema" ]; then
  echo "Creating backup of \"${database}\" database"

  mysqldump ${additional_mysqldump_params} --user=${mysql_user} --password=${mysql_password} ${database} | gzip > "${backup_dir}/${database}.gz"

  chmod 600 "${backup_dir}/${database}.gz"
  • Update the above mysql settings with your own username and password.
  • The backup files will be stored under /var/backups/mysql/{date}. You could change it with your own.

Step 3. Ensure the script is executable with following command:

chmod u+x backup.sh

Step 4. Execute the backup.sh.


You will find the backup files under /var/backups/mysql fold.


Backup all of databases automatically every day

Step 1. Login into security shell of your web server with ssh client.

Step 2. Run following command:

crontab -e

Step 3. Add following cron job into it:


It means run the backup script at middle night every day. All of the output from this script will be saved into backupdb.log file to check later.

Migrate database

For example, if you need to migrate a database named tomatocart into a new database – toc. You could finish it as follows:

Step 1. Find the database gzip file under /var/backups/mysql.

Step 2. Run following command to uncompressed it.

gzip -d tomatocart.gz

You will find a tomatocart file.


Step 3. Run following command to migrate the data.

mysql -hlocalhost -uroot -proot toc < tomatocart

As you can see, i init the toc database with tomatocart database. Please use your own username and password.


Database is the most important thing for your store. If you don’t backup them, i think it is dangerous for your ecommerce business because you will definitely have one day to lost the data. So, just backup your database since today.

Looking for quality TomatoCart hosting? Check out Arvixe Web Hosting

Tags: , | Posted under TomatoCart | RSS 2.0

Author Spotlight

Jack Yin

TomatoCart Developer & Co Founder - Arvixe Web Hosting / TomatoCart Community Liaison

Leave a Reply

Your email address will not be published. Required fields are marked *