Posted on

Rotating backup my self hosted wordpress sites with a bash script and a cron job

Getting hacked sucks! I have never been able to recover from the last hack that I suffered.
So Disaster Recovery is a priority. The last couple of days I’ve rebuild my blog on my own Linux server with NGINX, PHPFPM, and MySQL.
Today I went ahead and created 4 folders, one for each week of the month, assuming 4 weeks per month.

I wrote a quick backup.sh script, and setup a cron job with :

cd “$(dirname “$0″)”
value=$(cat count)
value=$(($value%4 + 1))
echo $”tar -czf /opt/backups/week$value/www.tgz /var/www”
tar -czf /opt/backups/week$value/www.tgz /var/www
echo $”mysqldump –all-databases | gzip > /opt/backups/week$value/data.gz”
mysqldump –all-databases -u root -p $MYSQL_PASSWORD | gzip > /opt/backups/week$value/data.gz
echo $value > count

This script reads from a file, in order to keep count of which week it currently is. So you will need that in the same directory.

Then I used this command to install the cron job

crontab -e

And used this to make it run every Thursday at 2 am.

# m h dom mon dow command
0 2 * * 4 /opt/backups/backup.sh

Next step is actually testing DR process by simulating a disaster and recovery.

Here is a video if you need more information:

Posted on

How to get free SSL certificates quickly installed on your Linux / NGINX server.

If you’re not aware of the letsencrypt project, let me teach you!
This wonderful organization is basically handing out free money, in the form of SSL certificates.
So if you’re savvy enough to self host your sites, you should be able to do this pretty quickly also.

If you’re like me and you have snap package manager and nginx installed all you have to do is:

  1. SSH into your server
  2. Run these commands
      sudo snap install --classic certbot
      sudo ln -s /snap/bin/certbot /usr/bin/certbot
      sudo certbot --nginx

This will install CERTBOT, create a symlink to it, and execute it for nginx. It really is that simple! I did make a quick video on it if you want to see more in depth explanation on how it worked for me.