How to Automatically Transfer VestaCP Backups to Google Drive
With this blog, I will tell you how to automatically transfer your vestacp backup files to your Google Drive. We will also learn how to automatically delete your backup files after they reach a certain number.
First of all, if you don’t have a Google Drive account yet, please visit drive.google.com to create a new one.
1. Step : Install google-drive-ocamlfuse
sudo add-apt-repository ppa:alessandro-strada/ppa
sudo apt-get update
sudo apt-get install google-drive-ocamlfuse
if you are getting an error like that when adding repository
sudo: add-apt-repository: command not foundYou can use this command
sudo apt-get install software-properties-common
2. Step : Authorize Google Drive Account
- 1. Go to Google Cloud Console with this link https://console.cloud.google.com/cloud-resource-manager
- 2. Create New Project
- 3. Give your project a name and confirm the action
- 4. Please search google drive api with search bar input
- 5. Please select “Manage” but if you are using for the first time, button text should be “Activate”
- 6. Credentials > Create Credentials
if you getting error, you have to create “OAuth consent screen” before step 5
- 7. Select “OAuth client ID”
- 8. Select “Desktop Application”
- 9. You will get “Client ID” and “Client Secret”
- 10. Go to console in your Linux server and exec this command
google-drive-ocamlfuse -headless -label me -id IDENTIFIER -secret SECRET
and you will get this message
Please, open the following URL in a web browser:
https://accounts.google.com/...
- 11. Please copy that link to your browser and you will see Google Auth screen. Finally you have access this app on browser.
- 12. When you accepted the auth with google, it will give you a code and take it and paste your console
Please enter the verification code:
- 13. Now there will be a new folder “google_dir” associated with your Google drive.
google-drive-ocamlfuse -label me /google_dir
3. Step : Google Drive Transfer
cd google_dir
mkdir backup
crontab -e
- Add the following line to the end of the cron file
04 01 * * * 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 01:04, 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.
4. Step : Delete Old Backup Archives
sudo nano /etc/delete-backup.shcd ~/google_dir/backup
ls -t | sed -e '1,10d' | xargs -d '\n' rm
- Then back to cron file with
crontab -e
And you will add new line that cron file like this for deleting old backups
05 01 * * * sudo bash /etc/delete-backup.sh
The above command will be executed at 01:05 on server time, on behalf of the administrator, the files in the folder “/google_dir/backup/” will be deleted leaving the 10 of the most recent ones untouched.