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:
#!/bin/bash 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="127.0.0.1" # instead 127.0.0.1 - 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" ftp -n $FTP_HOST <<END_SCRIPT quote USER $FTP_USER quote PASS $FTP_PWD binary put ./dump-$CURRENT_TIMESTAMP.tar quit END_SCRIPT rm -f "./dump-$CURRENT_TIMESTAMP.tar" "./mysqldump.sql" ndays=7 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 quote USER $FTP_USER quote PASS $FTP_PWD binary ls quit EOMYF ` lista=( $listing ) for ((FNO=0; FNO<${#lista[@]}; FNO+=9));do if [ ${lista[`expr $FNO+5`]}=$MM ]; then if [[ ${lista[`expr $FNO+6`]} -lt $DD ]]; then echo "Removing ${lista[`expr $FNO+8`]}" ftp -i -n $FTP_HOST <<EOMYF2 quote USER $FTP_USER quote PASS $FTP_PWD binary delete ${lista[`expr $FNO+8`]} quit EOMYF2 fi fi done 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.