Creating a BASH backup script

In case of problems with the site or hosting, the project administrator should be able to quickly return the site to working condition. For these purposes, you can periodically create a duplicate of site data: files and databases. This process is called backup, or backup. If there is a backup, the site administrator will at any time restore the functionality of the broken site or, in some cases, transfer the site to another server.

Backing up is important but time consuming if done manually. Therefore, they try to automate the creation of a backup, especially when the site is often updated and a duplicate must be created daily.

We made the backup process free and made it possible to use only the built-in server capabilities without third-party software.

In this article, you will be given a script to perform all the routine backup work.
This script allows you to create a backup every day and store them for 7 days, then it will delete the oldest backup on the 8th day of the script execution

Script configuration:


WWW_PATH=/var/www/html             # instead /var/www/html - Site directory for backup

FTP_HOST="FTP_IP"                  # instead FTP_IP - FTP server IP address
FTP_USER="FTP_USERNAME"            # instead FTP_USERNAME - FTP username
FTP_PWD="FTP_PASSWORD"             # instead FTP_PASSWORD - FTP user password

DB_HOST=""                # instead - Database Server IP
DB_USER="DB_USERNAME"              # instead DB_USERNAME - Database User
DB_PWD="PASSWORD"                  # instead PASSWORD - Database Password

CURRENT_TIMESTAMP=$(date +%Y-%m-%d-%H-%M)

mysqldump -A -u "$DB_USER" -h "$DB_HOST" -p "$DB_PWD" --ignore-table=mysql.innodb_index_stats --ignore-table=mysql.innodb_table_stats > "./mysqldump.sql"

tar -zc --file "./dump-$CURRENT_TIMESTAMP.tar" "$WWW_PATH" "./mysqldump.sql"

put ./dump-$CURRENT_TIMESTAMP.tar

rm -f "./dump-$CURRENT_TIMESTAMP.tar" "./mysqldump.sql"


MM=`date --date="$ndays days ago" +%b`
DD=`date --date="$ndays days ago" +%d`

echo removing files older than $MM $DD

listing=`ftp -i -n $FTP_HOST <<EOMYF 
lista=( $listing )

for ((FNO=0; FNO<${#lista[@]}; FNO+=9));do
    if [ ${lista[`expr $FNO+5`]}=$MM ];
        if [[ ${lista[`expr $FNO+6`]} -lt $DD ]];
            echo "Removing ${lista[`expr $FNO+8`]}"
            ftp -i -n $FTP_HOST <<EOMYF2 
            quote USER $FTP_USER
            quote PASS $FTP_PWD
            delete ${lista[`expr $FNO+8`]}

exit 0

The comments to the script indicate all the parameters that you need to specify for its correct performance.

Running the script:

The launch occurs through cron: for this, our hosting panel has a special section with the CRON setting - "Scheduler".

The main use of cron is to execute tasks at specific times, as shown below. This will execute the backup script we wrote on June 10th at 8:30 am.
Please note that the time field uses a 24 hour format, so 8 AM is 8, 8 PM is 20 hours.

30 08 10 06 * /home/developer/name_of_your_script

30th - 30th minute
08 - 08 AM
10th - 10th day
* - every day of the week

This script performs a simple task: it creates files so that a site that is broken for some reason can be returned to a working state. You can take this bash script as a basis and supplement it with your own code according to your needs. For writing bash scripts on the Internet, you can find many manuals with ready-made code examples.

12 April 2021

You may be interested in

21 October 2021
How to improve the accessibility of web page content fo ...
This article discusses ways to improve the accessibility of web page content for all users, includin ...
15 October 2021
Yandex vs Google by SERP
Let's compare how well Google and Yandex find the required information using a stationary computer.
15 July 2020
What is wmid
WMID is the identification number of the Web money payment system. If you do not already have your O ...