Office Hours
Day Hours (GMT)
Bank Holidays Closed
Monday 9:00 - 17:30
Tuesday 9:00 - 17:30
Wednesday 9:00 - 17:30
Thursday 9:00 - 17:30
Friday 9:00 - 17:30
Saturday Closed
Sunday Closed
Knowledgebase: Developer Articles
How to create a shell bash script for full database and file backups
Posted by Al Brookbanks on 20 October 2016 12:32 PM

To create a full backup of your store over SSH with a bash script please login via the terminal and in the root of your store create a file called "backup.sh" with the following content. 

#!/bin/sh
DBSERVER=XXX
DATABASE=XXX
USERNAME=XXX
PASSWORD=XXX
SQLFILE=${DATABASE}.sql
BACKUPFILE=full_ssh_backup_`date +"%d%b%y-%H%M%S"`.zip

echo "Creating MySQL dump ${SQLFILE}."
mysqldump --no-create-db=true -h ${DBSERVER} -u ${USERNAME} --password=${PASSWORD} ${DATABASE} > ${SQLFILE}
echo "Creating compressed archive of store files and ${SQLFILE}."
zip -rq ${BACKUPFILE} --exclude=${BACKUPFILE} --exclude=backup/* --exclude=images/cache/* --exclude=cache/* ./
echo "Backup created please download it from the 'Maintenance' > 'Backup' section of your admin control panel."
rm -rf ${SQLFILE}
mv ${BACKUPFILE} ./backup/${BACKUPFILE}
du -h ./backup/${BACKUPFILE}

Replace the red XXX values with your MySQL details and save this file to your server. To execute it via command line just type the following command:

./backup.sh

!!IMPORTANT SECURITY NOTICE!!
This file must not be accessible via a browser URL! To protect it edit your .htaccess file so that there is a section that looks like this:

## File Security
<FilesMatch "\.(htaccess|sh)$">
Order Allow,Deny
Deny from all
</FilesMatch>

Alternatively (or aswell as) please remove the "PASSWORD" value and change the mysqldump command so that it prompts for password on eack backup like this:

mysqldump --no-create-db=true -h ${DBSERVER} -u ${USERNAME} -p ${DATABASE} > ${SQLFILE}

Scheduling Backups
You could also create a cron job to execute this file daily.


©2017 CubeCart Limited. All rights reserved. CubeCart Limited is a company registered in England & Wales under number 5323904. VAT Registration Number GB886451190.