In this small post we would see , how we could sync Local directory through Google Drive.So this way we could save our on Google drive that would available to anytime from anywhere.

For this setup, we are using CentOS7 Linux machine. But Any Linux machine having Gdrive installed would work.

So we have one Backup directory which is having some backup files which we like to store on remote location, so in case something happened to our local server, in that case our backup file would safely relocated to another remote destination, which would help us to build same application in short duration.

Right now, our Google drive is empty

#gdrive list
Id   Name   Type   Size   Created

We are creating some directories to sync backup in Google Drive.

#gdrive mkdir Apache
Directory 0B4fN0sGuyby_ZERvUHM0ZDFqb00 created

#gdrive mkdir Database
Directory 0B4fN0sGuyby_U3F3TF91djBSTlk created

#gdrive list
Id                             Name       Type   Size   Created
0B4fN0sGuyby_U3F3TF91djBSTlk   Database   dir           2017-08-02 02:54:57
0B4fN0sGuyby_ZERvUHM0ZDFqb00   Apache     dir           2017-08-02 02:54:46

Now we have created two directories Apache and Database. Let’s start syncing backup files from local Disk to Google Drives.

#gdrive sync upload /backup/apache/ 0B4fN0sGuyby_ZERvUHM0ZDFqb00
Starting sync...
Collecting local and remote file information...
Found 5 local files and 0 remote files

5 remote files are missing
[0001/0005] Uploading apache_2017-07-30-02:00:01.tar.gz -> Apache/apache_2017-07-30-02:00:01.tar.gz
[0002/0005] Uploading apache_2017-07-31-02:00:01.tar.gz -> Apache/apache_2017-07-31-02:00:01.tar.gz
[0003/0005] Uploading apache_2017-08-01-02:00:01.tar.gz -> Apache/apache_2017-08-01-02:00:01.tar.gz
[0004/0005] Uploading apache_2017-08-02-02:00:01.tar.gz -> Apache/apache_2017-08-02-02:00:01.tar.gz
[0005/0005] Uploading backup.logs -> Apache/backup.logs
Sync finished in 15m52.608181065s

Let’s create some other file to see what happen with new files. Creating two empty files

#touch /backup/apache/f{1,2}

Let’s start sync once more on same directory.

#gdrive sync upload /backup/apache/ 0B4fN0sGuyby_ZERvUHM0ZDFqb00
Starting sync...
Collecting local and remote file information...
Found 7 local files and 5 remote files

2 remote files are missing
[0001/0002] Uploading f1 -> Apache/f1
[0002/0002] Uploading f2 -> Apache/f2             
Sync finished in 22.074325288s    

Let’s do some changes in one of above file. See what happen with change files.

#cat > /backup/apache/f1
Google Drive test file

#cat /backup/apache/f1
Google Drive test file

Let’s start again sync.

#gdrive sync upload /backup/apache/ 0B4fN0sGuyby_ZERvUHM0ZDFqb00
Starting sync...
Collecting local and remote file information...
Found 7 local files and 7 remote files

1 local files has changed
[0001/0001] Updating f1 -> Apache/f1
Sync finished in 1.238593718s

Now let’s delete some files. Here we are deleting f2 file from local sync directory.

#rm -rf /backup/apache/f2

#ls  /backup/apache/f2
ls: cannot access /backup/apache/f2: No such file or directory

Let’s start sync again.

#gdrive sync upload /backup/apache/ 0B4fN0sGuyby_ZERvUHM0ZDFqb00
Starting sync...
Collecting local and remote file information...
Found 6 local files and 7 remote files
Sync finished in 434.568324ms

oh, no. What happened. It didn’t delete remote file, that got deleted from local directory. No, actually for this we have another options that should used here.

#gdrive sync upload  --delete-extraneous /backup/apache/ 0B4fN0sGuyby_ZERvUHM0ZDFqb00
Starting sync...
Collecting local and remote file information...
Found 6 local files and 7
 remote files

1 remote files are extraneous
[0001/0001] Deleting Apache/f2
Sync finished in 817.7742ms

So it can also delte extra remote which already deleted from local directory.

So It’s great tool to save backup for small remote sites, This way we can save our backup