Page tree

Welcome to FreeSoftwareServers Confluence Wiki

Skip to end of metadata
Go to start of metadata

#Backup WordPress with Crontab

#Download this Backup-WP-Short.sh and use Find/Replace in a text editor

#Read my short Restore WP Website from TAR and SQL Dump guide

#Backup WordPress with Crontab

##You can make a copy of script and use a program (I use GEdit) to FIND AND REPLACE to easily convert to working copy-paste code!
#[database] with WP MySQL database name
#[domain] with domain
#[username] with MySQL real username
#[passwd] with password for MySQL username#[backuptarget] without trailing /
#I recommend filling in bottom part as you go along!

 

1. Create Exclude file for excluding Files/Folders from Backup

mkdir -p [backuptarget]/[domain]/tars

touch [backuptarget]/[domain]/exclude_wp.sh
nano [backuptarget]/[domain]/exclude_wp.sh && chmod 600 [backuptarget]/[domain]/exclude_wp.sh && sudo chown $USER:nogroup [backuptarget]/[domain]/exclude_wp.sh

 

--You should just create the exclude_wp.sh and not use if you don't want to, this command will work if file exists and is empty, but at a later date you can add to it without modifying Crontab. (The TAR command will fail if file does not exist!)

Example exclude_wp.sh

Lets say site is /var/www/html/domain/ & and the file tree is

/var/www/html/domain/file1
/var/www/html/domain/file2
/var/www/html/domain/file3
/var/www/html/domain/folder1
/var/www/html/domain/folder2
/var/www/html/domain/folder3

You want to exclude file1 and folder 1

exclude_wp.sh contents would be:

file1
folder1

OR
folder1
file1
 

--Doesn't Matter, Simple eh

2. Create and test TAR statement

Example File Tree:

Lets say your WordPress install is @

/var/www/html/domain/wp-content # And all the other files

Then your TAR command would be:

tar -czf [backuptarget]/[domain]/tars/"$(date '+%F').tar.gz" -C /var/www/html/ [domain]/ -X [backuptarget]/[domain]/exclude_wp.sh

OR

tar -czf [backuptarget]/[domain]/tars/"$(date '+%F').tar.gz" -C /var/www/ html/ -X [backuptarget]/[domain]/exclude_wp.sh

3. Create password file to prevent the need for user input when accessing MySQL via CLI

mkdir [backuptarget]/[domain]/sqldumps
touch [backuptarget]/[domain]/.sqlpwd
nano [backuptarget]/[domain]/.sqlpwd && chmod 600 [backuptarget]/[domain]/.sqlpwd && sudo chown $USER:nogroup [backuptarget]/[domain]/.sqlpwd
--.sqlpwd contents
[mysqldump]                 # NEEDED FOR DUMP
user=[username]
password=[passwd]

[mysql]                     # NEEDED FOR RESTORE
user=[username]
password=[passwd]

4. Create SQL statement and test!

--SQL CLI Syntax

mysqldump --defaults-extra-file=~/wp_backups/[domain]/.sqlpwd [database] > ~/wp_backups/[domain]/sqldumps/"$(date '+%F').sql"

5. Create FIND statement to find and remove all backups older then 1 week.
*Warning this statement removes all files older then one week under ~/wp_backups/[domain]/tars/ & ~/wp_backups/[domain]/sqldumps/

find ~/wp_backups/[domain]/sqldumps -mtime +7 -delete && find ~/wp_backups/[domain]/tars -mtime +7 -delete

 

6. Create Script to be called by Crontab

touch ~/wp_backups/[domain]/backupwp.sh && nano ~/wp_backups/[domain]/backupwp.sh && sudo chmod 700 ~/wp_backups/[domain]/backupwp.sh && sudo chown $USER:nogroup ~/wp_backups/[domain]/backupwp.sh

--backupwp.sh contents *Example*:

#!/bin/bash



tar -cfv ~/wp_backups/[domain]/tars/"$(date '+%F').tar" -C /var/www/html/ domain/ -X ~/wp_backups/[domain]/exclude_wp.sh && mysqldump --defaults-extra-file=~/wp_backups/[domain]/.sqlpwd [database] > ~/wp_backups/[domain]/sqldumps/"$(date '+%F').sql" && find ~/wp_backups/[domain]/sqldumps -mtime +7 -delete && find ~/wp_backups/[domain]/tars -mtime +7 -delete

 

OR

 #!/bin/bash

TAR STATEMENT
SQL STATEMENT
FIND STATEMENT

 

Try running script via

 ./backup.sh

 

and if SQL and TAR files show up, delete them to test Crontab later!

7. Create Crontab Entry:

 crontab -e

 

First Test via: *Run every 2 minutes*

 */2 * * * * ~/wp_backups/[domain]/backupwp.sh

 

Should see both TAR and SQL dump show up in under 5 minutes

Change Crontab Entry to: *Run @ 5 AM Daily*

 * 5 * * * ~/wp_backups/[domain]/backupwp.sh

 

#(Optional) Fill In here for Reference

1. EXCLUDE_WP.SH:

2. TAR STATEMENT:

3. .SQLPWD:

4. SQL STATEMENT:

5. FIND STATEMENT:

6. --SCRIPT-- *backupwp.sh*

#!/bin/bash

TAR STATEMENT
SQL STATEMEN
FIND STATEMENT

7. CRONTAB ENTRY:

Example file tree once done:

~/wp_backups/                       # Where ALL Domain/WordPress Site backups go
~/wp_backups/[domain]/              # Domain/WordPress Site specific directory
~/wp_backups/[domain]/exclude_wp.sh # Files and Folders to exclude from Domain TAR backup
~/wp_backups/[domain]/.sqlpwd       # SQL Username and PWD on WP Database for Website
~/wp_backups/[domain]/backupwp.sh   # Final Backup Script to be called by CRON
~/wp_backups/[domain]/tars/         # Where TARs of WordPress Directory are stored
~/wp_backups/[domain]/sqldumps/     # Where SQL dumps of the SQL database are stored

 

#Backup WordPress with Crontab
##You can make a copy of script and use a program (I use GEdit) to FIND AND REPLACE to easily convert to working copy-paste code!#[database] with WP MySQL database name#[domain] with domain#[username] with MySQL real username#[passwd] with password for MySQL username#[backuptarget] without trailing /#I recommend filling in bottom part as you go along!

  • No labels

1 Comment