Edit: gdrive has been updated to V2. This new post covers using it.
I built a DokuWiki instance, but I needed a good backup strategy in case of disaster. I figured a nice safe way will be to upload a backup of the wiki files to my Google Drive every day, as I'm still working on setting up a Linux server backup solution locally.
There's a nice little cli tool I found called gdrive, which lets you simply transfer files to your Google Drive with scripts. It doesn't do any syncing, and that's perfectly fine for this task. We're only uploading files for this exercise.
It also uses OAuth2, which saves you needing to store credentials on the machine, and the ability to revoke the token at a later time. Win-win.
Here's how I installed it:
wget -O drive https://drive.google.com/uc?id=0B3X9GlR6EmbnMHBMVWtKaEZXdDg mv drive /usr/sbin/drive chmod 755 /usr/sbin/drive
Yup. Very anti-climatic installation. That google drive link is just copied from https://github.com/prasmussen/gdrive#downloads.
Now, simply run
drive to start the authentication process. You'll get a link like this to paste and browse to in to your web browser:
Go to the following link in your browser: https://accounts.google.com/o/oauth2/auth?client_id=123456789123-7n0vf5akeru7on6o2fjinrecpdoe99eg.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&state=state
Authenticate and provide permission for the application to access your Google Drive, and then you'll be provided a verification code to copy and paste back in to your shell:
Enter verification code: 4/9gKYAFAJ326XIP6JJHAEhs342t35LPiA5QGW0935GHWHy9
Once that's done,
drive should be ready to use from the cli.
Creating a Backup Script
Now, all we need to do is run a daily job to zip up the DokuWiki directory, encrypt it and then upload it to Google Drive for storage. Create a bash script called
dokuwiki-backup.sh and fill with the following:
#!/bin/sh # tar the dokuwiki directory tar -zcf "dokuwiki-backup-$(date '+%Y-%m-%d').tar.gz" /var/www/dokudata/ # encrypt the tar openssl aes-256-cbc -a -salt -in "dokuwiki-backup-$(date '+%Y-%m-%d').tar.gz" -out "dokuwiki-backup-$(date '+%Y-%m-%d').tar.gz.enc" -pass 'pass:ReAlLyLoNgAnDcOmPlExPaSsWoRd' # remove the original tar rm -rf "dokuwiki-backup-$(date '+%Y-%m-%d').tar.gz" # upload to google drive drive upload --file "dokuwiki-backup-$(date '+%Y-%m-%d').tar.gz.enc"
The file should be named with today's date, in the format
2015-06-11). Don't forget to replace the password parameter with something that you've generated.
Creating a Cron job to run the Script
Change the permissions to allow only root to execute the script (I have mine living in a
chmod 500 /scripts/dokuwiki-backup.sh
Then add a new entry to
crontab, to run the script daily at 2am:
echo "0 2 * * * root /scripts/dokuwiki-backup.sh" >> /etc/crontab
It's as easy as that. Don't forget to also execute the script with
sh /scripts/dokuwiki-backup.sh just to test it.