opened image

Performing site and database backups with a simple script through the scheduler

Benefits of performing backups through the scheduler.

Executing site and database backups using a script through the scheduler has several advantages:

1. Automation of the process: The Scheduler allows you to run the script for execution on a schedule, which ensures the automation of the process of creating backups. You can set the desired time and frequency of backups, and the scheduler will run the script automatically, without the need to manually intervene in the process.

2. Ease of use: Using a script to create backups through the scheduler makes it easy to configure and change backup settings in one place. You can configure the script so that it backs up not only the site, but also the database, as well as set the necessary parameters, such as the location for storing copies, file format, and other options.

3. Flexibility and control: Using the script through the scheduler gives you the flexibility to choose options and settings for performing backups. You can select the directories, files and database tables you need to back up, as well as determine how often and where to save the created copies. This allows you to gain maximum control over the process of creating backups and adapt it to your needs.

4. Data safety: Regularly creating backups using a script through the scheduler helps to ensure the safety of your data. If there is a system crash, accidental deletion or damage to files or database, you can always restore data from the latest backup. This is important to prevent information loss and keep your site running smoothly.

5. Saving time and resources: Automated backups using a script and scheduler save time and resources that you could spend on manual backups. Scheduled backups free you from the need to constantly monitor the process and perform it manually. This allows you to focus on other tasks and makes better use of your time.

All in all, using a script through the scheduler to perform site and database backups offers automation, convenience, flexibility, and security that helps keep your data safe and makes the process of creating and managing backups easier.

Script examples.

You can set up backups using the following cron commands.

For example, in /home/admin/ or another folder of another user USER is your backup.sh script:

#!/bin/bash
curlftpfs ftp://userNAME:PASSWORD@IP/backup/ /backup
rm -f /backup/backup.tar.gz
rm -f /backup/backup.sql.gz
tar -czvf /backup/backup.tar.gz /home/admin/web
mysqldump db-NAME | gzip -c -9 > /backup/backup.sql.gz
umount /backup

To execute it, you need to assign a rule for its execution:

chmod +x backup.sh

The backup.sh script will do the following:

  •      Mounting FTP storage with curlftpfs.
  •      Removing old backups of the site and database on storage.
  •      Archiving the current state of the data.
  •      Creating backups of all sites / site in the /home/admin/web directory and the db-NAME database on the mounted FTP storage.
  •      After all operations are completed, the FTP storage folder will be unmounted.

Note that you need to replace userNAME and PASSWORD with your actual FTP server credentials, and also replace db-NAME with your database name. Where IP is the IP address of the FTP server. This script does not pre-check the availability of free space on the FTP server. Therefore, there should be plenty of space on the FTP server.

When setting the script launch via crontab, there is a hint about the values:

nano /etc/crontab 

To run backups every Tuesday at 21:00, add the following line:

00 21 * * 2 root sh /home/admin/backup.sh 

To run backups every hour, add the following line:

0 * * * * root sh /home/admin/backup.sh


 

You can also add this entire command to cron in one line to perform backups:

0 * * * * root curlftpfs ftp://userNAME:PASSWORD@IP/backup/ /backup && rm -f /backup/backup.tar.gz && rm -f /backup/backup.sql.gz && tar -czvf /backup/backup.tar.gz /home/admin/web && mysqldump db-NAME | gzip -c -9 > /backup/backup.sql.gz && umount /backup

You can also create your own script and put it in /etc/cron.daily/ for daily backups.

 

Using backups through the scheduler provides automation and regular backups of the site and database. The scheduler allows you to set a convenient backup schedule, ensuring data safety and loss protection. It is a convenient and flexible solution that saves time and resources by ensuring business continuity in the event of failures and unexpected events.