How to create hourly database backups in Plesk

If you have a website with important data, it is very important to take backups frequently. In most cases, hourly backup is very important.

I wrote a Bash Script for hourly backup for the database in Plesk.

Let’s move on to the steps.

1. Create directory and script

$ mkdir /root/mysql_backups && cd /root/mysql_backups
$ touch backup.sh
$ chmod +x backup.sh

2. Copy the code

Replace line 3 with your own database names.

#!/bin/bash

BACKUP_DATABASES=(YOUR_DATABASE1 YOUR_DATABASE2 YOUR_DATABASE3)

BASE="/root/mysql_backups"
RECENT_BACKUPS=($BASE/*/)

NOW=`date +%Y-%m-%d-%H`
ONE_HOUR_AGO=`date -d "1 hour ago" +%Y-%m-%d-%H`
TWO_HOURS_AGO=`date -d "2 hours ago" +%Y-%m-%d-%H`

for i in "${RECENT_BACKUPS[@]}"; do
	if [ "$BASE/$NOW/" = $i ] || [ "$BASE/$ONE_HOUR_AGO/" = $i ] || [ "$BASE/$TWO_HOURS_AGO/" = $i ]; then continue; fi;
	
	rm -rf "$i"
done;

# Create today named folder if not exists.
[ ! -d "$BASE/$NOW" ] && mkdir "/$BASE/$NOW"

# Backup databases
for i in "${BACKUP_DATABASES[@]}"; do  
	/usr/sbin/plesk db dump $i | gzip > "$BASE/$NOW/${i}.sql.gz"
done;

3. Paste via vim

$ vim backup.sh

4. Create hourly scheduled task

  1. Go to Tools & Settings > Scheduled Tasks > Add Task
  2. Paste the command: /root/mysql_backups/backup.sh
  3. Select Hourly option
Create hourly scheduled task

Done! Now the script runs every hour and backup your selected databases.

List folders on terminal
1 Shares:
Leave a Reply

Your email address will not be published. Required fields are marked *