Backups for Wordpress on DO

Categories: dev | Posted on Aug 22, 2014

So since I moved to DigitalOcean I no longer have automated Backup (I honestly think their current solution for it is a bit shitty and I don’t need a backup of all the things), I needed to come up with my own.

I’m just backing up my WordPress directory and the database on a daily basis, so should anything go horribly wrong I should be able to go back.

Here is a Bash script I’m running via a cronjob.

#!/bin/bash
NOW=$(date +"%Y-%m-%d")
FILE="wp_$NOW.tar"
DBFILE="wp_db_$NOW.sql"
 
BACKUPDIR="/home/CHANGEME/backups"      # Where are Backups saved?
TOBACKUP="/var/www"                     # What should be backed up?
 
DBUSER="CHANGEME"                      # SQL DETAILS
DBPASS="CHANGEME"                      # CHANGE ALL OF THESE
DBNAME="CHANGEME"                      # TO YOUR STUFF
 
# Get all files
tar -cf $BACKUPDIR/$FILE $TOBACKUP
 
# Get database
mysqldump -u$DBUSER -p$DBPASS $DBNAME > $BACKUPDIR/$DBFILE
 
# Put everthing togeter
tar --append --file=$BACKUPDIR/$FILE -C $BACKUPDIR $DBFILE
rm $BACKUPDIR/$DBFILE
 
# And compress
gzip -9 $BACKUPDIR/$FILE

Also, I’m deleting every backup which is older than 7 days with this snippet:

@daily find /home/hopps/backups* -mtime +7 -exec rm {} \;

At last I pull these files to a different location via rsync, but I have not yet finished that part. I will moreover put in on a seafile server, so I have it available on like every machine.