Backup to Google Drive

Legacy

This will be my attempt at documenting what I did when configuring automatic backup of websites and databases to Google Drive. Note that this is in past tense, ergo I have already completed this project and will now try to backtrack what I actually did…

First of all, this requires a shell account where your website is hosted.

I started with how to upload to Google Drive. Previously Lemon had setup this for me on the old VPS-installation, but since I believe the only way to learn is to do it yourself (or fuck it up yourself), I’ve since reinstalled the VPS. I did use some of his code, which is something I could have written myself, but I’m lazy so I copy+pasted. Anyway, I have no idea what he used to connect to Google Drive, so I started fresh.

https://github.com/prasmussen/gdrive
After 100+ tabs in Chrome I decided upon gdrive. It offers way too much more than I need, but seeing as it’s coded by a fellow Norwegian, I’m guessing it reeks of quality… Either way, it was the simpler choice.

I used wget to copy the file (which is hosted at Google Docs for some reason). Now, this is not my forte, but eventually I realized that for it to work as I had planned, I had to copy it to /usr/local/bin, though looking back this might not have been the case. Either way. That’s where it is now. Also, when I downloaded the file via wget, it got a terrible name, so I renamed it to gdrive. Though, I did this via WinSCP… Running gdrive the first time you have to enter “gdrive about”, this will prompt you with a link you need to open, which is where you select which Google Account to authorize access to. In return you get a key which you paste into gdrive (specifically asks for it). Voila, you now have ultimate unlimited power…

Now for the fun part… gdrive made no sense at all to me, making it impossible for me to use folders I had made in Google Drive. gdrive uses IDs. Which makes sense since Google Drive allows you to have the same name for files and folders in the same folder. So I made a folder. And then I made a folder inside that folder. Because I need structure. When you make a folder or upload a file (or anything) gdrive gives it an ID. If you want to meddle with any files or folders, you need to use that ID. And now, I have an ID for the sub-folder where I intend to store my backup.

The code below is a shell script (based on Lemon’s script). Basically we start with pre-defining domain-name, database-name, folder-location and ID of the previously created folder in Google Drive. It also sets time and date so that backup-files have the necessary information of when the backup is from. First off we dump the database. I created a user called dump, this way I can configure his privileges (grants) in hopes of added security. I’ve also created a file to store the password in, so that I don’t have to keep the password in neither shell script nor crontab. No idea if this secure, but according to some of the 100+ tabs I had open, this was the right way to do it.

Moving on after the dump, we tar the files in the folder together with the database-dump. Having been born unto Windows, tar is new for me, but it’s just zip for Linux… The tar is named after the domain we are backing up and the time it was backed up. If you ever need to restore to a certain time, this is highly valuable information. After that we run gdrive with a simple upload to specific folder and delete after upload. I prefer to delete it after upload. I’ll be taking a backup at least once per week, and the more sites you have, the higher the gigabytes rack up. Also, if the VPS is hacked, explodes or whatnot, that is not where I need a backup. Keeping a backup in the same location as where the original files are is not a backup. It’s just duplicates and waste of space. I also delete the database-dump. Since this specific file is not uploaded directly by gdrive (it is inside the tar), gdrive is not deleting it.

Now I have the script that backups website files and database, names, tars, uploads and deletes. Tested and tried, it works as intended. So here I venture into crontab so that I can schedule this update. Logging in and running the script manually is stupid… For some reason crontab always opens in whatever the fuck it is, so I tend to start off with code below to get it in nano. I prefer nano, but to each their own. And then just enter the line below. Crontab will then run the script every 3rd day of the week (Wednesday) at 01:00. When adding more sites I will delay each script with 5-10 minutes to make sure it does not conflict with each other.