Posted on

Update Ubuntu server system time

Yesterday I setup a rotating backup for my wordpress sites that I am self hosting on an ubuntu server.

Had to update the system time on my server to make sure my cron jobs actually execute when I expect them to. I ran the following commands:

sudo timedatectl set-timezone America/Los_Angeles
sudo hwclock –systohc
sudo timedatectl set-ntp true
sudo systemctl restart cron
sudo systemctl restart rsyslog

this updated the cron daemon and the syslog daemon to use the new timesettings, which I set to Los Angeles time, and also made sure they are synced with an NTP server.

I also edit the logging system configuration by running

vi /etc/rsyslog.d/50-default.conf

Then uncommenting the line

cron.* /var/log/cron.log

This allows me to follow the tail of the log file and make sure that my cron job is executed at the expected time without any errors

tail -f /var/log/cron.log

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: