Vesta CP

How to Automatically Transfer VestaCP Backups to Google Drive

Today we will talk about transferring backups of your sites hosted in VestaCP to Google Drive. After all, we all know that storing backups in only one place is a bad idea. Server failure, hackings, administrative errors can lead to irreversible loss of data, and no one wants to lose their brainchild, and for this, the presence of a fresh backup in a safe distant place adds confidence in the future.

 

Requirements:

  1. Google Drive Account
  2. VestaCP Installed on Your Server
  3. We are Using Ubuntu 18.04 for this tutorial

 

Must Read:

 

 

How to Transfer VestaCP Backups to Google Drive

So for this purpose, we are going to use google-drive-ocamlfuse. It is a FUSE file system for Google Drive, written in OCaml. It lets you mount your Google Drive on Linux.

 

Step 1: Install google-drive-ocamlfuse

Install google-drive-ocamlfuse on your Linux server with VestaCP Installed. With this program, we will connect Google Drive to our server and will be able to manage files directly from Linux CLI.

sudo add-apt-repository ppa: alessandro-strada / ppa
sudo apt-get update
sudo apt-get install google-drive-ocamlfuse

Step 2: Once you have successfully installed google-drive-ocamlfuse on your server the next step is to authorize and access your Google Drive Account.

  1. Follow the link to Google Developers Console and log in with your Google Account – https://code.google.com/apis/console/
  2. Create a new project and give it a name.
  3. Now search for the Google Drive API in the API Library and activate it.
  4. Go to the “Credentials” tab, create a new “OAuth Client Identifier“.
  5. An error will appear “To create an OAuth client identifier, specify the product name for the access request window” → click “Configure OAuth access request window” → Enter only the name → Save → Select “Other types” in the list → Create.
  6. Now we have an identifier and a secret. Go to the console in your Linux server and execute the below code replacing IDENTIFIER and SECRET with your keys.
    google-drive-ocamlfuse -headless -label me -id IDENTIFIER
    -secret SECRET
  7. Next, you will get a link to authorize your Google account, copy and paste the link from your console window into the browser.
    Please, open the following URL in a web browser: https: //accounts.google.com / ...
  8. Complete the authorization, next is to create a folder and connect it with Google Drive.
    mkdir / google_dir
    google-drive-ocamlfuse -label me /google_dir

     

  9. Now there will be a new folder “google_dir” associated with your Google drive.

 

 

Step 3: Automate Google Drive Transfer

In this step, we will automate the transfer of backup archive to Google drive by scheduling cron tasks.

1. First, create a folder inside /google_dir folder named backups, this will be used as temporary storage until the transfer process completes.

sudo mkdir /google_dir/backup/

2. Enter the below command (a choice of text editors may appear, I advise you to choose nano or vim which you have installed):

crontab -e

 

3. Add the following line to the end of the cron file:

44 2 * * * sudo cp -R -n /backup/*.tar /google_dir/backup/

 

Now you can configure the archive transfer schedule by modifying the above line with your cron execution time. For example in my scenario at 2:44, by server time, on behalf of the administrator, files matching the * .tar mask will be copied to the “google_dir/backup/” folder, files that are already found in the target folder will not be copied. If you have many users, and you need only one backup, then it would be wise to change the file mask to admin *.tar.

Now make sure that VestaCP backups are turned on as this will only use those backups created by VestaCP in backups and will transfer them in your Google Drive folder as per your cron schedule.

Note: You can also paste the above command in cron settings provided by VestaCP.

 

 

Step 3: Deleting Old Backup Archives

In this process, you can configure on how much backup cycle or for days your backup will retain on Google drive and after that, it will automatically delete older archives.

  1. To delete old backups, we will use a script. Create a new file named delete-backup.sh inside the /etc directory.
sudo nano /etc/delete-backup.sh

 

2. Make the file executable.

sudo chmod ugo + x /etc/delete-backup.sh

 

3. Schedule the automatic execution. Add the below code inside the file that you created.

10 2 * * * sudo /etc/delete-backup.sh /google_dir/backup/ * tar 7

 

The above command will be executed at 2:10 on server time, on behalf of the administrator, the files in the folder “/google_dir/backup/” matching the mask “* tar” will be deleted leaving the 7 of the most recent ones untouched.

 

That’s all, the above steps will automate your Backups to Google Drive. If you face any problem following the steps or need any help just leave your queries in the comments below.