A simple script to backup WordPress each day on an Ubuntu server and keep it for 7 days

Install the zip extension

sudo apt install zip

Create directory /var/backups

sudo mkdir /var/backups

Create file backup.sh and place scrip 

sudo vim /home/yourusername/backup.sh
#/bin/bash
wppath=/var/www/wp
backuppath=/var/backups
backupdatetime=date +\%Y-\%m-\%d-\%H-\%M-\%S
#----ACCESS TO DB ------#
DBNAME=dwordpress
DBUSERNAME=dbusername
DBPASSWORD=dbpassword

#----Backup of WordPress DB----#
mysqldump $DBNAME -u $DBUSERNAME -p$DBPASSWORD | gzip > /var/backups/wordpress-db-$backupdatetime.sql.gz

#----Backup of WordPress Files----#
zip -r $backuppath/wordpress-data-$backupdatetime.zip $wppath

Add to cron tab file:

sudo crontab -e
4 10 * * * /bin/bash /home/yourusername/backup.sh
2 2 * * * find /var/backups/* -mtime +7 -exec rm {} \;

Source: https://github.com/devlionco/bashscripts/blob/master/backup-wordpress-files.sh

Leave a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.