Create a backup script for WordPress with a Rapsberry Pi
Create a backup script for WordPress with a Rapsberry Pi
Foreword
Whether you want to automate a blog or website backup you should be aware the following method isn’t the best solution. This backup script is simple to make and helpful to save server logs or for server migration.
When it comes to large files take a look to rsync that updates the backup repository without re-downloading the whole content. Files are checked and only new files are added to the backup folder.
I made this single-use script because i want to migrate my website location. The server location has a SEO impact and this hosting service is located in the USA. Most subjects on this server, at this moment, are related to Europe so I prefer to share my website for European traffic mostly.
Hands-on
SFTP configuration
First of all, you must have access to your web server trough the sftp protocol. Usually hosting providers give you an access to ssh thus you can use sftp for transferring files.
Once you have an sftp access you have to add the ssh key in the ~/.ssh/known_hosts file onto your raspberrypi. This step allows you to connect to your server without typing your password.
Manually you have to generate a key and place it on both side which means on the RapsberryPi and on the webserver.
ssh-keygen -t rsaEnter file in which to save the key (/home/arnaud/.ssh/id_rsa): Enter passphrase (empty for no passphrase):
Type enter to let the default location. Then let the passphrase empty by typing enter again.
The configuration is almost finished. The ultimate step is to copy the public key into the new machine’s authorized_keys file.
ssh-copy-id user@ip
As usual you have to write yes and your credentials in the terminal :
The authenticity of host '198.0.100.2 (198.0.100.2)' can't be established. RSA key fingerprint is a3:d4:55:97:af:66:7a:f3:f7:88:d3:0c:42:81:23:56. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '198.0.100.2' (RSA) to the list of known hosts. user@198.0.100.2's's password:
The key is added on the webserver.
crontab configuration
The second part is to use crontab to save regularly our data. Cron is a tool for configuring scheduled tasks on Unix systems. It is used to schedule commands or scripts to run periodically and at fixed intervals.
By typing : crontab -e, cron will ask you to choose an editor to open the crontab file. The arguments to build a job task are described in the file.
# Example of job definition: # .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | | # * * * * * user-name command to be executed
Let’s say we want to execute the backup script on a sftp server every Sunday at 17:30 :
# m h dom mon dow command 30 17 * * SUN sftp user@ip 'bash -s' < /home/pi/Documents/backup.sh && cd ~/Downloads && tar -cvzf backup.tar.gz arnaudf.eu/ logs/ --remove-files
This cronjob connects the RaspberryPi to the web server and tells it to execute the commands contained in backup.sh.
#!/bin/bash #backup.sh lcd ~/Downloads get -R blog/ get -R logs/ exit
Therefore, the blog/ and logs/ repositories will be uploaded to the raspberrypi and then compressed inside the Downloads/ folder.
Be careful to give a date corresponding to RaspberryPi’s time-zone for time arguments ! To check your time-zone type date in the prompt.
:~/Documents $ date Sat 2 May 10:05:17 UTC 2020
In case you have some issues with cron you can try to execute this cronjob to figure out what is the problem. This task calls the date command every minute and appends it in datelog.txt.
* * * * * date >> /home/pi/Documents/datelog.txt