Post

Sync Google Drive in Linux using rclone

Google Drive doesn’t have any official linux client. Over the years many unofficial method was used to integrate google drive in linux. Mounting as file system or KDE and Gnome’s Google Drive mount. But none of them sync like official client in windows. What we want is two way sync.

We will achieve this with rclone feature called bisync.
Make sure to install rclone in your linux distro Using sudo apt install rclone or sudo dnf install rclone depending if you use debian based system or fedora.

  1. First step is we need to login to Google Drive. The initial setup for drive involves getting a token from Google drive which you need to do in your browser. rclone config walks you through it.
    Here is an example of how to make a remote called remote. First run:

    1
    
    rclone config
    

    This will guide you through an interactive setup process:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    
    No remotes found, make a new one?
    n) New remote
    r) Rename remote
    c) Copy remote
    s) Set configuration password
    q) Quit config12/24/23
    n/r/c/s/q> n
    name> remote
    Type of storage to configure.
    Choose a number from below, or type in your own value
    [snip]
    XX / Google Drive
    \ "drive"
    [snip]
    Storage> drive
    Google Application Client Id - leave blank normally.
    client_id>
    Google Application Client Secret - leave blank normally.
    client_secret>
    Scope that rclone should use when requesting access from drive.
    Choose a number from below, or type in your own value
    1 / Full access all files, excluding Application Data Folder.
    \ "drive"
    2 / Read-only access to file metadata and file contents.
    \ "drive.readonly"
    / Access to files created by rclone only.
    3 | These are visible in the drive website.
    | File authorization is revoked when the user deauthorizes the app.
    \ "drive.file"
    / Allows read and write access to the Application Data folder.
    4 | This is not visible in the drive website.
    \ "drive.appfolder"
    / Allows read-only access to file metadata but
    5 | does not allow any access to read or download file content.
    \ "drive.metadata.readonly"
    scope> 1
    Service Account Credentials JSON file path - needed only if you want use SA instead of interactive login.
    service_account_file>
    Remote config
    Use web browser to automatically authenticate rclone with remote?
    * Say Y if the machine running rclone has a web browser you can use
    * Say N if running rclone on a (remote) machine without web browser access
    If not sure try Y. If Y failed, try N.
    y) Yes
    n) No
    y/n> y
    If your browser doesn't open automatically go to the following link: http://127.0.0.1:53682/auth
    Log in and authorize rclone for access
    Waiting for code...
    Got code
    Configure this as a Shared Drive (Team Drive)?
    y) Yes
    n) No
    y/n> n
    --------------------
    [remote]
    client_id = 
    client_secret = 
    scope = drive
    root_folder_id = 
    service_account_file =
    token = {"access_token":"XXX","token_type":"Bearer","refresh_token":"XXX","expiry":"2014-03-16T13:57:58.955387075Z"}
    --------------------
    y) Yes this is OK
    e) Edit this remote
    d) Delete this remote
    y/e/d> y
    
  2. Then we will create a test file called RCLONE_TEST. We will put this file in google drive root directory and as well as our local directory where we are going to sync. We are doing this check access. This is a rclone feature for safety measure.
    You can create this file as touch RCLONE_TEST from terminal. Copy this file to our root directory of Google Drive and the local directory. Ex: ~/google-drive/RCLONE_TEST
  3. Next we will sync our drive in a local folder. Let’s create a mydrive folder in our home directory.
    1
    
    mkdir -p ~/mydrive
    

    Now use the following command:

    1
    
    rclone bisync "remote:/" "/home/zihad/mydrive/" --check-access --fast-list --drive-skip-shortcuts --drive-acknowledge-abuse --drive-skip-gdocs --drive-skip-dangling-shortcuts --verbose --resync
    

    Note that we are skipping Google docs. Because rclone has yet to resolve the issue.
    If everything was correct you will see your google drive content in the mydrive directory. Change to your username.

  4. Now let’s automate the command using systemd. Create ~/.config/systemd/user directory if it doesn’t exist.
    1
    
    mkdir -p ~/.config/systemd/user
    

    In the directory create a file called sync-google-drive-rclone.timer. And paste below:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    [Unit]
    Description=sync google drive with rclone every 5 minutes.
       
    [Timer]
    OnCalendar=*:0/05
    Persistent=true
       
    [Install]
    WantedBy=timers.target
    
    

    Note that 0.05 means we will sync our directory every 5 minutes.

    Create another service file called sync-google-drive-rclone.service. Paste below code, Make sure to change username:

    1
    2
    3
    4
    5
    
    [Unit]
    Description=sync google drive with rclone every 5 minutes.
       
    [Service]
    ExecStart=rclone bisync "remote:/" "/home/zihad/mydrive/" --check-access --fast-list --drive-skip-shortcuts --drive-acknowledge-abuse --drive-skip-gdocs --drive-skip-dangling-shortcuts
    

    Let’s enable the service from command line.

    1
    2
    
    systemctl enable --user sync-google-drive-rclone.timer
    systemctl start --user sync-google-drive-rclone.timer
    

    Test if the service is enabled.

    1
    
    systemctl list-timers --user
    

    That’s it now google drive will sync in every 5 minutes.

This post is licensed under CC BY 4.0 by the author.