5

I want to create a bash script that will log into my google drive account and backup my main folder. I've done a bit of research and I know we need some sort of OAuth for using the Google Drive API but I'm still fairly new so I thought I can get some solid guidance from the stack community.

jiggunjer
  • 1,905
  • 1
  • 19
  • 18
halapgos1
  • 1,130
  • 4
  • 16
  • 34

1 Answers1

13

I guess you don't need to re-invent the wheel, use gdrive and you're good to go

SETUP:

wget -O drive "https://drive.google.com/uc?id=0B3X9GlR6EmbnMHBMVWtKaEZXdDg"  
mv drive /usr/sbin/drive  
chmod 755 /usr/sbin/drive 

Now, simply run drive to start the authentication process. You'll get a link like this to paste and browse to in to your web browser:

Go to the following link in your browser:  
https://accounts.google.com/o/oauth2/auth?client_id=123456789123-7n0vf5akeru7on6o2fjinrecpdoe99eg.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&state=state 

Authenticate and provide permission for the application to access your Google Drive, and then you'll be provided a verification code to copy and paste back in to your shell:

Enter verification code: 4/9gKYAFAJ326XIP6JJHAEhs342t35LPiA5QGW0935GHWHy9  

USAGE:

drive upload --file "/some/path/somefile.ext" 

SRC:

Backing up a Directory to Google Drive on CentOS 7


NOTE:

If you really want to build a bash script take a look at this gist , i.e.:

  • automatically gleans MIME type from file
  • uploads multiple files
  • removes directory prefix from filename
  • works with filenames with spaces
  • uses dotfile for configuration and token
  • interactively configuring
  • uploads to target folder if last argument looks like a folder id
  • quieter output
  • uses longer command line flags for readability
  • throttle by adding curl_args="--limit-rate 500K" to $HOME/.gdrive.conf

#!/bin/bash
# based on https://gist.github.com/deanet/3427090
#
# useful $HOME/.gdrive.conf options:
#    curl_args="--limit-rate 500K --progress-bar"


browser="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36"

destination_folder_id=${@: -1}
if expr "$destination_folder_id" : '^[A-Za-z0-9]\{28\}$' > /dev/null
then
    # all but last word
    set -- "${@:0:$#}"
else
    # upload to root
    unset destination_folder_id
fi

if [ -e $HOME/.gdrive.conf ]
then
    . $HOME/.gdrive.conf
fi

old_umask=`umask`
umask 0077

if [ -z "$username" ]
then
    read -p "username: " username
    unset token
    echo "username=$username" >> $HOME/.gdrive.conf
fi

if [ -z "$account_type" ]
then
    if expr "$username" : '^[^@]*$' > /dev/null || expr "$username" : '.*@gmail.com$' > /dev/null
    then
        account_type=GOOGLE
    else
        account_type=HOSTED
    fi
fi

if [ -z "$password$token" ]
then
    read -s -p "password: " password
    unset token
    echo
fi

if [ -z "$token" ]
then
    token=`curl --silent --data-urlencode Email=$username --data-urlencode Passwd="$password" --data accountType=$account_type --data service=writely --data source=cURL "https://www.google.com/accounts/ClientLogin" | sed -ne s/Auth=//p`
    sed -ie '/^token=/d' $HOME/.gdrive.conf
    echo "token=$token" >> $HOME/.gdrive.conf
fi
umask $old_umask

for file in "$@"
do
    slug=`basename "$file"`
    mime_type=`file --brief --mime-type "$file"`
    upload_link=`curl --silent --show-error --insecure --request POST --header "Content-Length: 0" --header "Authorization: GoogleLogin auth=${token}" --header "GData-Version: 3.0" --header "Content-Type: $mime_type" --header "Slug: $slug" "https://docs.google.com/feeds/upload/create-session/default/private/full${destination_folder_id+/folder:$destination_folder_id/contents}?convert=false" --dump-header - | sed -ne s/"Location: "//p`
    echo "$file:"
    curl --request POST --output /dev/null --data-binary "@$file" --header "Authorization: GoogleLogin auth=${token}" --header "GData-Version: 3.0" --header "Content-Type: $mime_type" --header "Slug: $slug" "$upload_link" $curl_args
done
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • it should be `drive about` on first launch, to set the key https://github.com/prasmussen/gdrive#other . Also, i think the command has changed to `gdrive`, althought `drive` still works – AlexanderD Jul 16 '17 at 13:47
  • 1
    gdrive is not maintained since September 2017 – reducing activity Nov 01 '18 at 11:29
  • 1
    And that's a reason to downvote? The answer is from 2015. – Pedro Lobito Nov 01 '18 at 12:40
  • 2
    As of September 2019 Gdrive is still working very well. Google didnt change its API, so, Gdrive keeps doing its job. Since it's a very simple and bare tool (Gdrive's command line options are not what you would call "friendly") it seems to me that it doesnt requires "maitenance". Maybe it could be "enhanced", but no "maintained". – mguima Oct 08 '19 at 13:19
  • 4
    This is not working , it says App is disabled by google – Random I.T Apr 27 '20 at 01:10