in ,

WordPress Database Auto Backup Using Linux Crontab And Bash Script

wordpress database backup

WordPress database backup is a very important task to care about, and as we talk in the previous post, we need to stop adding extra plugins to our WordPress and need to keep it as light as possible, you can read: Optimize WordPress Database Daily Using Linux Crontab

Each task can be separated away from WordPress, and transferred to the system side it will be a good action to apply.

WordPress Database Backup Policy

We need to apply the following backup policy

1- Take a database backup daily

2- Backup time will be at 1 AM

3- Remove Backup files older than 3 days.

And you can adjust your backup policy as you need, then implement it in the bash script like the following…

Database Backup Bash Script

We create a new  file, insert the script as shown with the description, and replace each bold green value with yours.


# Database credentials
## Add database username
## Add database password
## Add database server Hostname or IP Address
## Add database name

# Backup path
current_date=$(date +"%d-%b-%Y")

# Dump the database
/usr/bin/mysqldump --user=$db_user --password=$db_password --host=$db_host $db_name > $db_backup_path/$db_name-$current_date.sql

# Delete database backup files older than 3 days
/bin/find $backup_path/* -mtime +3 -exec rm {} \;

Crontab The Database Backup Script.

Enable your script file for execution, then open the crontab command for edit

# chmod +x
# crontab -e

And append the database backup script at the end as the following to run daily at 1 AM.

0 1 * * * /full/path/to/ >/dev/null 2>&1

Any way to adjust your script running date-time with crontab, you can use the tool.

What do you think?

Leave a Reply

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

      Optimize WordPress Database

      Optimize WordPress Database Daily Using Linux Crontab

      Mame Romset

      14083 Working MAME Romset