Backup Linux Servers to Google Drive

Ron MalmsteadSr. Developer
CERTIFIED EXPERT
Published:
Updated:
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give you a link to get 115 GB for free for two years.
Step One - Install Google Drive
wget -O drive https://drive.google.com/uc?id=0B3X9GlR6EmbnMHBMVWtKaEZXdDg 
                      mv drive /usr/sbin/drive 
                      chmod 755 /usr/sbin/drive
                      drive

Open in new window


You will see a link in your terminal, which you can paste into your browser.  You may be prompted to login to Google.  Next give Gdrive allow permissions, then Copy/Paste the code from the page into the terminal.  Google drive command is now ready.



Step Two - Backup the boot partition directly to Google Drive

In just two lines of code, you can send the boot partition image directly to Google Drive, on-the-fly.

BOOTDRIVE="$(fdisk -l | grep '^/dev/[a-z]*[0-9]' | awk '$2 == "*"' | cut -d ' ' -f 1)"
                      sudo dd conv=sparse if=${BOOTDRIVE} | gzip -c --fast | drive upload --stdin --title bootdrive.gz

Open in new window


....and that's it! ..but.. a little explanation helps though right?
"dd" pipes the raw image data of your boot partition into "gzip", which compresses that data, then pipes "|" the compressed result into "drive upload".

Note: Large boot partitions, which is unusual but possible..can take considerable time depending on the speed of your internet connection.  The "conv=sparse" option will zero empty space, making the resulting file much smaller compressed  than the actual size of the partition; however, in some cases it may be necessary to resize the partition, backup, then resize back to the full available space.  Or, you can split the image using the methods described in Step Four.


Step Three - Backup your Linux installation

The below lines of code will backup your "/" root folder and place the backup file on the root of the drive.

If your drive does not have at least 50% free space, you may have to choose an alternate location, include command switches to use higher compression (slower), or add exclusions for large files you don't necessarily need to backup.

cd /
                      sudo tar -cvpzf backup.tar.gz --exclude=/backup.tar.gz --one-file-system /

Open in new window


Step Four - Send the backup file to Google Drive

Depending on the size of the backup file, it may be necessary to split it into smaller files (I recommend 2GB files).  Files larger than 2GB have a higher likelihood of getting timed out when transferring to Google Drive, and you won't know it unless you are regularly creating and examining  logs of each step using " >> Logfile.txt", stdout after each command.

If the file is regularly failing to transfer in full, due to the size, then you should split it...otherwise transfer the file as a whole using...

drive upload -f backup.tar.gz

Open in new window


To split the file use,

mkdir /backup
                      mv backup.tar.gz /backup
                      cd /backup
                      split -b 2000M backup.tar.gz "root_backup_"
                      rm -f backup.tar.gz
                      find . /backup -printf "drive upload -f %p\n" | bash

Open in new window


Step Five - Remove the local copy of the backup file.

rm -f backup.tar.gz

Open in new window


, or if you used the split method you can just delete the /backup folder

cd /
                      rm -rf /backup

Open in new window


--------------------------------------------------------------------------------------------------------------------------

Once you are familiar with this backup process, you might want to automate it with crontab.

It is worth noting that you do not have to create a boot image backup separate from the root folder backup.  You can image the entire drive (usually /dev/sda), and split it into smaller files for transfer.  However, I created these steps separately because in most restore circumstances, a full restore of the entire disk isn't necessary.  When deploying this backup solution, you should also take note of "fdisk -l" output, so you know what partitions were on the system and their sizes in the event you have to restore the entire system.  You would have a much easier restore process if you setup the disk partitions to match what you had originally prior to restoring the data on them.

If you split your files, and you need to restore them... you will need to recombine them first, using...

cat root_backup_* > backup.tar.gz

Open in new window

1
14,627 Views
Ron MalmsteadSr. Developer
CERTIFIED EXPERT

Comments (2)

Michele CapobiancoSenior Application Engineer

Commented:
Great Tutorial!

One little question. Is it possible to upload into a certain google drive folder?
I checked the Docs, but i didn't find anything..

Thanks Capo
Ron MalmsteadSr. Developer
CERTIFIED EXPERT

Author

Commented:
I believe you would need to use the option -p

-p, --parent <parent>     Parent id, used to upload file to a specific directory, can be specified multiple times to give many parents


To get the folder id you can use 'drive list'

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.