Skip to content

Backup script – How write a script to back up website and database on Linux

Last updated on December 17, 2023

I am not going to go around, let’s go to the point. This is all the script that I will talk about.

#!/bin/bash

BASE_DIR='/home/tommydo/Documents/dump'
IMG_DIR='/var/www/mywebsite/wp-content/uploads/'
TODAY=`date +%Y%m%d-%H`
YESTERDAY=`date -d '-1 day' +%Y%m%d-%H`
FEWDAYAGO=`date -d '-2 day' +%Y%m%d-%H`
WEEKAGO=`date -d '-7 day' +%Y%m%d-%H`

if [ ! -d ${BASE_DIR}/${TODAY} ]
then
    mkdir ${BASE_DIR}/${TODAY}
    chmod -R 777 ${BASE_DIR}/${TODAY}
fi

cd ${BASE_DIR}/${TODAY}

mysqldump -u {user_name} -p{password} --databases {database_name} --add-drop-database --add-drop-table --no-tablespaces > dump.sql

cp -r ${IMG_DIR} .
cd ${BASE_DIR}
zip -r ${TODAY}.zip ${TODAY}

cp -r ${TODAY}.zip /media

if [ -d ${BASE_DIR}/${FEWDAYAGO} ]
then
    rm -rf ${BASE_DIR}/${FEWDAYAGO}
    rm ${FEWDAYAGO}.zip
fi

Firstly, we create a file and name it to whatever you want, I named it as backup.sh. One thing you should remember that the file need to be “.sh” extension. This file type is executable file on Linux system.

Put this line !/bin/bash on top of file. Ohhh…wait. What is it? Okay, you can read this article for more information. (#!/bin/bash ) What exactly is this?

BASE_DIR='/home/tommydo/Documents/dump'
IMG_DIR='/var/www/mywebsite/wp-content/uploads/'
TODAY=`date +%Y%m%d-%H`
YESTERDAY=`date -d '-1 day' +%Y%m%d-%H`
FEWDAYAGO=`date -d '-2 day' +%Y%m%d-%H`
WEEKAGO=`date -d '-7 day' +%Y%m%d-%H`

These lines above, I define some constants that we will use. For instant, when execute commands in Linux, we usually use path of directories. To avoid to use it repeatedly, I defined it as BASE_DIR='/home/tommydo/Documents/dump'. Next, I define TODAY to get current date time as text and I will put it into backup file’s name.

Attention ! When I define constant to store directory path, I use Apostrophe – single quote ‘ However, on timing constant, I use Grave Accent – backtick `. See ‘ and ` they are different.

So why I use single quote for path. This is because I just define all text inside. Otherwise, with TODAY I put date – this is a Linux function and all magic characters go with date function is its parameter. When you execute date on Linux terminal it will show the output like:

tommydo@myserver:/$ date
Sat 10 Apr 2021 02:28:07 PM MDT

And when you execute with parameter.

tommydo@myserver:/$ date +%Y%m%d-%H
20210410-14

Yes, you are right. I formatted the original date time output into my format with %Y is 2021, %m is 04 (April), %d is 10 (10th day) and %H is 14 (2pm). You can also put minutes and seconds to it by %M and %S. To me, I just use until hour, it is enough.

To learn more about date function, read here: Date Command In Linux: How To Set, Change, Format And Display Date

if [ ! -d ${BASE_DIR}/${TODAY} ]
then
    mkdir ${BASE_DIR}/${TODAY}
    chmod -R 777 ${BASE_DIR}/${TODAY}
fi

Next, I am going to check that the directory name “20210410-14” which is defined into TODAY constant, is existed or not. Above lines of code are if else statement in Linux. I am not go too far with this. So if you want to know more about if else in Linux, read Bash if..else Statement

mysqldump -u {user_name} -p{password} --databases {database_name} --add-drop-database --add-drop-table --no-tablespaces > dump.sql

Above line is MySQL dump database command. It is fundamental command when you use MySQL. If you don’t know it, read here: Dumping Data in SQL Format with mysqldump

cp -r ${IMG_DIR} .

cd ${BASE_DIR}
zip -r ${TODAY}.zip ${TODAY}

cp -r ${TODAY}.zip /media

After dumping my database into folder “20210410-14”. I copy all uploaded images cp -r ${IMG_DIR} . into current directory. The dot . is represented for current directory.

Ok, now I did backup everything I need, so next, I compress them to ZIP file for moving around easily. This command cp -r ${TODAY}.zip /media I copy a alternative file to my external hard drive (USB) that I have already mounted it to /media

if [ -d ${BASE_DIR}/${FEWDAYAGO} ]
then
    rm -rf ${BASE_DIR}/${FEWDAYAGO}
    rm ${FEWDAYAGO}.zip
fi

Last but not least, to avoid saving to many backup files, I remove it regularly after 2 days.

All in all

That is all my backup script for my website on server Linux. To run the file, simply use

$ /bin/bash backup.sh

Of course, I won’t do it manually, I will ask server machine do backup regularly for me using crontab which is an app package on Linux as well. You can refer to learn Crontab usage guide at How to use cron in Linux.

Good luck and thank for reading.

Published in⌨️ Programming TipsBlogs

Comments are closed.