How to Backup your Linux Server with Google Drive (Step 3 of 3) [Remote Backup Script & Grive Script]

This is the last post for this how to.  Hopefully you have already read and completed the following posts:

Part 1 – Local backup script

Part 2 – Installing Grive

Now we create another script.  This script may be different depending on whether or not you decided to go with a separate backup server.  I am going to paste this entire script into my post and you just need to read anything past the # for any comments.

Create this script in etc/cron.hourly.  Make sure to take off the .sh and make it executable.

#!/bin/bash
#Rsync multiple servers then Google Drive Sync
#Manipulate this script to match your server names, usernames, and associated passwords. 
#SSH will also need to trust the connecting server, so make sure to test each of the SSHPASS lines below in an SSH session.  Another option would just be to have previously SSH'd to all of the connecting servers.

#EXAMPLEWEBserver1 - [websites and databases]
sshpass -p "PASSWORD" rsync -avs --delete [email protected]:/var/backups/files /root/GoogleDrive/Backups/EXAMPLEWEBserver1

#EXAMPLEWEBserver2 - [websites and databases]
sshpass -p "PASSWORD" rsync -avs --delete [email protected]:/var/backups/files /root/GoogleDrive/Backups/EXAMPLEWEBserver2

#EXAMPLENAMEserver1 - [main bind files]
sshpass -p "PASSWORD" rsync -avs [email protected]:/etc/named.conf /root/GoogleDrive/Backups/EXAMPLENAMEserver1
sshpass -p "PASSWORD" rsync -avs --delete [email protected]:/var/backups /root/GoogleDrive/Backups/EXAMPLENAMEserver1

# Directory to backup
BACKUPDIR=/root/GoogleDrive/Backups

# Google Drive directory
GDRIVEDIR=/root/GoogleDrive

# Directory target in remote
TARGETDIR=/Backups

# =====================================================================
# It is unlikely you will need to edit anything below this line.
# Config END

# Create backup dir if not exists
echo Creating ${GDRIVEDIR}/${TARGETDIR} if needed
if [ ! -d "${GDRIVEDIR}/${TARGETDIR}" ]; then mkdir ${GDRIVEDIR}/${TARGETDIR}; fi

# Moving to Gdrive Dir
echo Entering ${GDRIVEDIR}
cd ${GDRIVEDIR}

# Initial sync
echo Initial Google Drive Sync
grive

# Coping new content
echo Copying from ${BACKUPDIR}/* to ${GDRIVEDIR}/${TARGETDIR}/
cp -R ${BACKUPDIR}/* ${GDRIVEDIR}/${TARGETDIR}/

# Showing files copied
echo Files to sync
find ${GDRIVEDIR}/${TARGETDIR}/

# Final sync
echo Final Google Drive Sync
grive

Like I mentioned in the comments of the above script, you will need to run each individual SSHPASS line to build a relationship between each rsync server.

 

A possibly more simple approach would be to just initiate a remote SSH connection from the backup server.

root@backup:~# ssh EXAMPLEWEBserver1.yourdomain.com
The authenticity of host 'EXAMPLEWEBserver1.yourdomain.com (127.0.0.1)' can't be established.
ECDSA key fingerprint is 03:3e:79:2c:3e:bb:ea:8a:fa:39:30:86:1a:d1:9f:24.
Are you sure you want to continue connecting (yes/no)?
root@backup:~#

Each of those lines are pulling files down from the remote server onto a local drive so they can be synced with Google Drive.  If the line has –delete in the script it will clean up and local files that have since been deleted on the remote location.  This keeps files that are past that 5 day old length automatically cleaned up.  Otherwise you will end up syncing every file your server ever puts in its backup directory.

If this script is running properly that is all you need to do.  Backups should be showing up on your Google drive regularly!

**With this current setup files will not be automatically cleaned up from the trash in Google Drive.  I believe this can be fixed, so I am currently working on that.**

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.