WP backup strategy

WP backup strategy

This is my WP backup strategy:

The strategy involves backing up both the MySQL database and the WordPress file system. I have successfully migrated my site to different machines and restored from various failures, and this approach has proven reliable.

To back up the MySQL database, I installed automysqlbackup. This user-friendly tool simplifies the setup process and allows for flexible scheduling of daily, monthly, and yearly backups. It also offers additional features to enhance the backup process.

For the file system backup, I employ a script to save the Apache’s www folder to an external hard drive. Additionally, I utilize rsync to synchronize the files with another system. To further safeguard my files, I leverage the WPB2D WordPress plugin to back up to Dropbox.

#!/bin/sh
file_bckup(){
	BackupDir=filebackup
	cd /mnt/External/bckup/
	for f in *.tar.gz
	do
	filename=$f
	done
	
	HomeDir=/mnt/External/bckup/
	BackupYear=${f:14:2}
	BackupMonth=${f:11:2}
	
	# Test to see if the main backup directory exists if not then create it
	if [ ! -d $HomeDir/$BackupDir ]; then
	mkdir $HomeDir/$BackupDir
	fi
	
	# Test to see if the backup directory for the year exists, if not then create it.
	if [ ! -d $HomeDir/$BackupDir/$BackupYear ]; then
	mkdir $HomeDir/$BackupDir/$BackupYear
	fi
	
	# Test to see if the backup directory for the month exists, if not then create it.
	if [ ! -d $HomeDir/$BackupDir/$BackupYear/$BackupMonth ]; then
	mkdir $HomeDir/$BackupDir/$BackupYear/$BackupMonth
	fi
	
	#Step #02 - command to be executed:
	mv /mnt/External/bckup/*.tar.gz $HomeDir$BackupDir/$BackupYear/$BackupMonth/
}
#delete the old db files to avoid over growing
rm -rf /mnt/External/bckup/db/

#copy the db
cp -R /var/backups/db/ /mnt/External/bckup/db/

DAY=`date +%u`
# Checks to see if sunday, and if so, delete last week backup
if [ $DAY -eq "7" ]; then
cd /mnt/External/bckup/
rm *.tar.gz
fi
#######################################################################################
#create a folder and compress it then we can save the compress file on the memory stick!
cd /mnt/External/bckup/
mkdir web
#cp -R /var/www/ /mnt/External/bckup/web/ old way
rsync -avz --exclude 'webmin*' /var/www/ /mnt/External/bckup/web/ #this way exclude webmin folders

FILE=web.tar.gz            
NAME=${FILE%.*}
EXT=${FILE#*.} 
DATE=`date +%d-%m-%y`         
NEWFILE=${NAME}_${DATE}.${EXT}
 
tar -zcf $NEWFILE /mnt/External/bckup/web/

rm -rf web
#######################################################################################

DAY=`date +%u`
# Checks to see if sunday, and if so, backs up the last week
if [ $DAY -eq "7" ]; then
file_bckup
#cd /home/pi/Code
#./wakeonlan.py nas 
#sleep 2m
scp -r /mnt/External/bckup/  user@192.168.1.7:/mnt/primary/pi/SdCard
#ssh user@192.168.1.7 sudo shutdown -h now
fi

#./emailSend.py

I hope this code will be useful to others, enjoy!

Comments are closed.