August 1, 2020

WordPress: Backup script

I do not like plugins. I prefer the command line and a bit of common sense. This is the way I make backups of my whole web page and store them on my laptop.

#!/bin/sh
DATE=$(date +'%d_%m_%Y')

BKPDIR=/path/to/my/backups

# Remove the last file.tar.gz in the backup directory
ls -1t $BKPDIR/*tar.gz | tail -n +1 | xargs rm -f

# Dump the database
mysqldump --no-tablespaces --add-drop-table --host=blabla-bloblo.data.ie \
--user=dbo34568762 --password=MyAweSomePassWord db_name \
> DB_$DATE.sql

# Create a tar.gz of the whole directory where wp is
tar cvfz AstroGR_$DATE.tar.gz \
/path/to/my/wp/installation/AstroGR \
DB_$DATE.sql

# Remove the database file
rm DB_$DATE.sql

# Move it to the backup directory
mv AstroGR_$DATE.tar.gz $BKPDIR

# Instructions to scp

echo ""
echo "Now from your laptop go into ~/andromina/AstroGR_Backup/ and run SCP.sh"

And this is how the SCP.sh script looks like

#!/bin/sh
scp YOURSSHUSER@whatever.server.xyz:/abhorrent/path/backups/AstroGR_\*gz .