How To Automate Directory Database And Website Backup Using Bash

If there is anything that makes Linux a great server OS when it comes to maintenance, it is scripting.

Be it simple bash scripting, python or perl, it’s awesome when you can automate boring repetitive tasks, knowing that the system will handle everything perfectly.

From creating database dumps, syncing filesystem backups, to automating application installations, scripting is present everywhere in Linux.

In this tutorial, we will cover the basics of bash scripting, enabling you to write your own bash scripts you can use to make your life easier. Also checkout how to write bash functions.

Every bash script begins with a shebang (#!), followed by a path to the bash interpreter:

The below line is also called bash shebang.

#!/bin/bash

This will tell the shell which interpreter is to be used (bash, tcsh, zsh etc). While this path is correct for almost all systems, you can always verify it by issuing a simple command:

which bash

This will tell you exactly where your bash interpreter is:

[ box ~ ]$ which bash
/bin/bash

How To Archive Contents Of Directory Using Bash Script

Let’s say you would like your script to create an archive of all contents of a directory.

All it takes is a few lines of code in your script:

#!/bin/bash
tar -pczf /home/user/backups/archive.tar.gz /home/user/directory

While not at all mandatory, it is always smart to use commenting in our scripts, especially if they are large and complex. It’s easy to differentiate our scripts at the beginning, but experienced Linux system administrators sometimes have hundreds of ready, functional scripts that they are using on a multitude of servers, and it makes their life much easier when they have explanatory comments in them enabling them to know exactly which script to use. So, you should start smart and insert a few explanatory lines there, right bellow the shebang line (each commenting line must begin with a hash character - #):


#!/bin/bash
##########################        #       
#       our first simple archiving script    #
#                 created on 17.12.2019      #
###########################
tar -pczf /home/user/backups/archive.tar.gz /home/user/directory

We can now save our script as, for example, archiving-script.sh, but until we make bash executable, for the operating system it is just a plain text file. This is done by one simple line:

chmod +x /path/to/our/archiving-script.sh

From now on, our archiving-script.sh is executable, and we can check it’s functionality from the command line:

./archiving-script.sh

If you’ve read our tutorial on the Cron Jobs, you can now for example use your archiving script to create backups automatically, in pre-dettermined time intervals.

Variables

In next step of our tutorial we will introduce variables. Variables are temporarily stored pieces of information that we can use in our scripts. Without them it would not be possible to get into bit more complex scripting. As always, best way for explaining what variables are, is through examples.

Automating MySql Database Backup Using Cron

Let’s say we want to create a script for automating database backups with help of cron. Assuming you have MySql or MariaDB installed. We will define a few variables at the beginning (change highlighted words with your actual values):

#!/bin/bash
####################################
#                                                                             #
#       our second script, simple database dump          #
#                           with several variables                  #
#                           created on 17.12.2019.               #
#                                                                             #
####################################
DUMP=database.dump.`date +"%Y%m%d"`
DATABASE=OUR_DATABASE
USER=OUR_DB_USER
PASS=OUR_PASSWORD

mysqldump --opt --user=${USER} --password=${PASS} ${DATABASE} > ${DUMP}
gzip $DUMP

Save the script as, for example, db-backup.sh, make it executable as the previous one, and start it. If we did everything right, starting it will produce a gzipped, compressed new file in the current directory, labeled with database.dump.+today’s date.

Automating Wordpress Website Backup Using Bash Script

Now, let’s say we wanted to use this script for backing up a WordPress website. In that case, database is just one part of the story, because we need to backup website files as well.

We can play around, and use something like this:

#!/bin/bash
####################################
#                                                                             #
#       our third script, a complete website backup      #
#       with a database dump and all website’s files     #
#       named according to current time and date                  #
#                           created on 18.12.2019.               #
#                                                                             #
####################################
# naming an archive with current time and date
BACKUP=backup.`date +"%Y%m%d"`
# naming a database dump with time and date as well
DUMP=SQL.DATABASE.`date +"%Y%m%d"`

 

# website's mysql database
DATABASE=OUR_DATABASE
# website database user
USER=OUR_USER
# website database user's password
PASS=OUR_PASSWORD
# website files location
FILES=/var/www/
# creating a database dump
mysqldump --opt --user=${USER} --password=${PASS} ${DATABASE} > $DUMP
# create a compressed tar-gzipped archive of all website files and a website database dump
tar -pczf $BACKUP.tar.gz $DUMP $FILES
# clean up a little bit
rm -f $DUMP

We can now continue and schedule a cron job, which will create a complete backup of all website’s files and it’s database, every night at, let’s say, 2AM (ideally, you should use your website analytics system, to determine when your website has least visitors, and schedule a backup at that time, especially if you have a huge website with lots of files and a large database, because archiving can be quite resource consuming. Fast servers with a lot of resources should not have a lot of problems with that, but it all depends on a lot of factors.).

If you’ve read our tutorial on cron jobs, you should know how to do it.

If not, read on…

We will save our script as, for example, website-backup.sh, create a directory intended for backups, move our script there and make it executable as we previously did with chmod +x command:

mkdir /home/user/backups
mv website-backup.sh /home/user/backups
cd /home/user/backups
chmod +x website-backup.sh

Then, invoke the crontab editor with crontab -e command, and schedule a cron job to run every night at 2AM.

0 2 * * * /home/user/backups/website-backup.sh

Once done, just save the file and exit the text editor. I am using vim.

This will run our script every night at 2AM. You can, as we mentioned, adjust the time according to your website load. By changing the second column to any value from 0 to 23, you are changing the hour when your script will be executed. You can also for example set it to run once a week by changing the fifth column, or why not - set it to run twice a day by changing the second column by appending a comma and an additional value like this:

0 2,14 * * * /home/user/backups/website-backup.sh

This will trigger the script both at 2AM and at 2PM (14).

You can freely explore additional options and play around, and if you get stuck we recommend you go through our Cron Jobs tutorial where everything is nicely explained.

That’s all for this part of the tutorial, see you in next one soon.

Related Topics:

bash if file exists

bash for loop range bash array of strings