Backup Magento project files / DB using the bash script

I have googled for the bash script to backup Magento sites/DB but none of them worked for me the way I wanted.
So I decided to create a custom bash script which is simple and does the job perfectly.

I have developed the script for my own need. But I thought it would be helpful if shared with you guys as well.
Here goes the overall backup script:

#@author		MagePsycho <[email protected]>
#@version		0.1.0

#/************************ EDIT VARIABLES ************************/
#/************************ //EDIT VARIABLES **********************/

host="$(echo "cat /config/global/resources/default_setup/connection/host/text()" | xmllint --nocdata --shell $dbXmlPath | sed '1d;$d')"
username="$(echo "cat /config/global/resources/default_setup/connection/username/text()" | xmllint --nocdata --shell $dbXmlPath | sed '1d;$d')"
password="$(echo "cat /config/global/resources/default_setup/connection/password/text()" | xmllint --nocdata --shell $dbXmlPath | sed '1d;$d')"
dbName="$(echo "cat /config/global/resources/default_setup/connection/dbname/text()" | xmllint --nocdata --shell $dbXmlPath | sed '1d;$d')"

fileName=$projectName-$(date +"%Y-%m-%d")
printf "What kind of backup you would like?\n[ d ] DB backup only\n[ f ] Files backup only\n[ b ] Files backup with DB\n"
read backupType
if [[ $backupType = @(d|b) ]]; then
	echo "----------------------------------------------------"
	echo "Dumping MySQL..."
	mysqldump -h $host -u $username -p$password $dbName > $fileName.sql
	echo "Done!"

if [[ $backupType = @(f|b) ]]; then
	echo "----------------------------------------------------"
	echo "Archiving Files..."
	printf "Skip /media folder?\ny: Yes\nn: No\n"
	read skipMedia
	if [ $skipMedia == y ]; then
		tar -zcf $fileName.tar.gz --exclude=var --exclude=includes --exclude=media * .htaccess
		tar -zcf $fileName.tar.gz --exclude=var --exclude=includes * .htaccess
	echo "Done!"
	echo "----------------------------------------------------"
	echo "Cleaning..."
	rm -f $fileName.sql
	echo "Done!"

if [[ $backupType = @(d|f|b) ]]; then
	echo "----------------------------------------------------"
	mkdir -p $backupDir;
	echo "Moving file to backup dir..."
	if [ $backupType == d ]; then
		mv $fileName.sql $backupDir

	if [[ $backupType = @(f|b) ]]; then
		mv $fileName.tar.gz $backupDir
	echo "Done!"
	echo "Invalid selection!"

Or you can download it from [here]
Notes: If you get the following error:

syntax error in conditional expression: unexpected token `(‘
line 24: syntax error near `@(d’
line 24: `if [[ $backupType = @(d|b) ]]; then’

then this means you are using an older version of bash (< 4.0). And you need to patch the script by adding the following line after bash script declaration:

shopt -s extglob

What does this script do?

  1. Gives options for the backup type which are:
    • DB backup only
    • Files backup only
    • Files backup with DB
  2. Dumps the database by taking DB info from XML configuration.
  3. Makes a copy of project files and compresses it in .tar.gz format.
    • While copying it also gives options whether to exclude media folder or not.
    • Note that by default ‘var’ & ‘includes’ folder are excluded by the script.
  4. Deletes the dumped SQL file as it’s already copied in the compressed file.
  5. Creates backup dir if not exists
  6. Copies the compressed project file to the backup dir.

Besides, you can also setup Cron job to run this backup script at regular intervals.

How to run the backup script?

1. Edit to configure variables ‘projectName’ & ‘backupDir’
2. Upload the edited to the root of your Magento installation
3. Run following series of commands in terminal:

cd /path/to/magento/root
chmod +x
ex -sc

4. You will get the compressed backup file at backup dir

Snapshots for different backup types

Magento DB backup only
Magento DB backup only
Magento Files backup only
Magento Files backup only
Magento Files backup with DB
Magento Files backup with DB

Let’s comment if there’s any room for improvement in this script.
Thanks for reading & sharing.

8 thoughts on “Backup Magento project files / DB using the bash script”

  1. Thanks for that great article!

    With my GNU bash, Version 4.3.11(1)-release I get a
    Syntax error: “(” unexpected (expecting “then”)

    Does someone knows what exactly is wrong?

    Thanks to everyone!

  2. Hello

    Is it working for windows command through CMD prompt?

    i got error by using “cd /www/demostore chmod +x ex -sc $’%s/r$//e|x’ sh”.

    error ref. attached. – Thanks

  3. Hi,

    thanks for this great script!

    I guess one has to run the commands 1-3 only the first time!

    for the next backup only the sh is needed? correct?

    is it save to leave the file on the server?

    how would the cron command look like? just



Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.