Auto backup Web Server to Google Drive

Rclone is a command line program to sync files and directories to and from:

  • Amazon Drive
  • Amazon S3
  • DigitalOcean Spaces
  • Dropbox
  • Google Cloud Storage
  • Google Drive
  • Mega
  • Microsoft Azure Blob Storage
  • Microsoft OneDrive
  • And more here 

Install Rclone

mkdir -p /home/backup

#Linux 64 bit:
cd /home/backup
wget https://downloads.rclone.org/v1.41/rclone-v1.41-linux-amd64.zip
unzip rclone-v*.zip
\cp rclone-v*-linux-amd64/rclone /usr/sbin/
rm -rf rclone-*

#Linux 32bit
cd /home/backup
wget https://downloads.rclone.org/v1.41/rclone-v1.41-linux-386.zip
unzip rclone-v*.zip
\cp rclone-v*-linux-386/rclone /usr/sbin/
rm -rf rclone-*

Check rclone:

[root@itfromzero backup]# rclone version
rclone v1.41
- os/arch: linux/amd64
- go version: go1.10

Create connection from server to Google Cloud

rclone config

At the command line still, type:

No remotes found – make a new one type n -> Enter

name> connection name: upload_google_drive -> Enter

Choose a number from below, or type in your own value
<Danh sách Cloud Server>
Storage>  type 11 -> Enter

client_id =  -> Enter
client_secret =  -> Enter

Scope that rclone should use when requesting access from drive. type 1 – Full access all files… -> Enter

root_folder_id = -> Enter
service_account_file = -> Enter

Use auto config? type n -> Enter

If your browser doesn’t open automatically go to the following link: https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=xxxxxxx

Open above link by browser, then login to google drive -> Choose Allow. Copy Code.

Enter verification code> Paste Code -> Enter

Configure this as a team drive? type n -> Enter

type y to accept and q to exit.

Check connection:

rclone lsd upload_google_drive:

Script auto backup and upload to google drive

Create backup.py

nano /home/backup/backup.py

Script Contents:

#!/usr/bin/python
# -*- coding: utf-8 -*-

#################################################################
#
# Web Server Backup and Upload to Google Drive
#
# Author: itfromzero.com
#
#################################################################
import os
from datetime import datetime
import logging, logging.handlers
import time

#Logging
def setup_custom_logger(name):
    formatter = logging.Formatter(fmt='%(asctime)s %(levelname)-2s %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
    handler = logging.handlers.RotatingFileHandler("/home/backup/backup.log", maxBytes=900000, backupCount=15)
    handler.setFormatter(formatter)
    screen_handler = logging.StreamHandler()
    screen_handler.setFormatter(formatter)
    logger = logging.getLogger(name)
    logger.setLevel(logging.DEBUG)
    logger.addHandler(handler)
    logger.addHandler(screen_handler)
    return logger


logger = setup_custom_logger('myapp')
MYSQL_LIST=['db1','db2'] #MYSQL Database
DB_NUM = len(MYSQL_LIST)
WEB_LIST=['a','b','c'] #WEB Folder name
WEB_NUM= len(WEB_LIST)
MYSQL_USER='' #MYSQL user
MYSQL_PW='' #MYSQL Password
GDRIVE_FOLDER='BACKUP_WEB'

logger.info('Web Server Backup Script')

#Create Backup Folder
logger.info('Create Backup Folder')
cmd = "mkdir -p /home/backup/`date +%Y%m%d`"
os.system(cmd)

logger.info('Database Backup')
for i in range(0,DB_NUM):
	cmd = "mysqldump -u "+MYSQL_USER+" -p"+MYSQL_PW+" "+MYSQL_LIST[i]+"  | gzip > /home/backup/`date +%Y%m%d`/"+MYSQL_LIST[i]+"-`date +%Y%m%d-%H-%M-%S`.sql.gz"
	os.system(cmd)
	logger.info('Backup database: %s Complete',MYSQL_LIST[i])

logger.info('Web Source Backup')
for j in range(0,WEB_NUM):
	cmd = "zip -r /home/backup/`date +%Y%m%d`/"+WEB_LIST[j]+".zip /home/www/"+WEB_LIST[j]+" -q -x /home/www/"+WEB_LIST[j]+"/wp-content/cache/**\*"
	os.system(cmd)
	logger.info('Backup Web Source: %s Complete',WEB_LIST[j])

logger.info('Web Server Config Backup')
cmd = "cp -r /etc/nginx/conf.d/ /home/backup/`date +%Y%m%d`/nginx/"
os.system(cmd)
logger.info('Backup Web Server Config Complete')

logger.info('Start Upload To Google Drive')
cmd = 'rclone move /home/backup/`date +%Y%m%d` "upload_google_drive:'+GDRIVE_FOLDER+'/`date +%Y%m%d`" >> /var/log/rclone.log 2>&1'
os.system(cmd)
logger.info('Upload Complete')

logger.info('Delete Backup File')

cmd = "rm -rf /home/backup/`date +%Y%m%d`"
os.system(cmd)
logger.info('Delete Complete')

logger.info("Remove all backups older than 3 days")
cmd = 'rclone -q --min-age 3d delete "upload_google_drive:'+GDRIVE_FOLDER+'"'
os.system(cmd) 
logger.info("Remove all empty folders older than 3 days")
cmd = 'rclone -q --min-age 3d rmdirs "upload_google_drive:'+GDRIVE_FOLDER+'"'
os.system(cmd) 
logger.info("Cleanup Trash")
cmd = 'rclone cleanup "upload_google_drive:"'
os.system(cmd)
logger.info("Finish Backup and Upload to Google Drive")

Make backup crond

crontab -e

Add:

#
# Crontab - Auto backup
#
0 3 * * * python /home/backup/backup.py > /dev/null 2>&1

Let’s enjoy!