Google Drive Sync for Obsidian

repository·master·Indexed 43 days ago

https://github.com/richardx366/obsidian-google-drive

An unofficial Obsidian plugin (version 3.1.1) that enables two-way synchronization between Obsidian vaults and Google Drive, supporting cross-device usage including iOS. It provides commands for pushing and pulling changes, automatic syncing options, and support for managing multiple independent vaults via a tagging system.

Tokens
1.9K
Snippets
1
Records
10
Agent score
7%

What's inside Google Drive Sync

  1. How multiple vaults work with Google Drive

    master

    The plugin allows you to manage multiple independent vaults within a single Google account using a tagging system.

    Vault Identification

    • When a vault is first set up, a folder is created in Google Drive named after your vault. This folder is tagged with the vault name and stores the name internally.
    • Each file in the vault is also tagged with the vault name in its Google Drive properties.
    • You can safely rename or change the color of the folder in Google Drive; syncing will not break.

    Managing Multiple Vaults

    • To sync multiple devices to the same vault: Ensure the local vault names are identical on all devices.
    • To create a new, separate vault: Create a local vault with a unique name. Do not simply rename an existing synced vault locally, as this will break the connection to its specific Google Drive folder. Instead, create a new vault, enable the plugin, and move files into it via the Obsidian app.
  2. Important usage constraints and precautions

    master

    To prevent data loss or sync errors, adhere to these rules:

    • Backup your vault: Always create a backup before using the plugin.
    • No manual uploads: Do NOT manually upload files into the generated Google Drive folder using other sync tools. The plugin cannot see these files and it may cause data loss.
    • No external file edits: Do NOT modify files outside of the Obsidian app. The plugin tracks changes via the Obsidian API; external changes will not be detected.
    • Avoid path conflicts: If you delete/rename a file with no extension and then immediately create a folder with the exact same path, run a Sync between those two actions to prevent errors.
    • Internet stability: Ensure a stable connection during sync. Closing the app or losing connection mid-sync can lead to data corruption.
    • Conflict resolution: The plugin uses local file prioritization to resolve conflicts automatically. There is no manual conflict resolution interface.
  3. Set up Google Drive Sync

    master

    To initialize syncing between Obsidian and Google Drive, follow these steps:

    1. Visit the plugin homepage at https://ogd.richardxiong.com.
    2. Click Sign In and authenticate with your Google account.
    3. Copy the refresh token provided after successful login.
    4. Enable the Google Drive Sync plugin in Obsidian.
    5. Paste the refresh token into the plugin settings.
    6. Reload the Obsidian app.

    Note: If you are setting up the plugin on a new vault for the first time, ensure the vault is empty. If you have existing files, move them to a temporary vault, activate the plugin in the empty vault, and then move the files back into the vault using the Obsidian app to ensure they are tracked correctly.

  4. Syncing changes between Obsidian and Google Drive

    master

    The plugin supports both pulling from and pushing to Google Drive:

    Pulling (Google Drive $\rightarrow$ Obsidian)

    • The plugin automatically pulls changes from Google Drive once when Obsidian finishes opening the vault.
    • This process pulls cloud files to your local device.
    • To trigger this manually, run the Pull from Google Drive command.
    • Note: Pulling new plugins or configurations may require an Obsidian restart.

    Pushing (Obsidian $\rightarrow$ Google Drive)

    • To sync local changes to the cloud, click the sync button on the ribbon or run the Push to Google Drive command from the command palette.
    • Pushing automatically performs a pull before pushing changes to ensure synchronization.
    • Recommendation: Sync before closing Obsidian to ensure the cloud state is up to date.

    Automatic Syncing

    • You can enable Automatically push changes in the plugin settings. When enabled, the plugin will push changes to Google Drive one minute after the most recent local file change. This is disabled by default.
  5. Migrating from version 2.x.x to 3.x.x

    master

    If you are upgrading from a 2.x.x version of the plugin to 3.x.x, you must perform the following steps to fix file system discrepancies caused by how deleted files were handled in older versions:

    1. Run the Fix Google Drive paths command.
    2. Run a sync.

    This process corrects Obsidian's understanding of the Google Drive file system and resolves common syncing errors.

  6. Configure Google Drive Sync Settings

    master

    The plugin is configured via the Obsidian Settings tab. Key settings include:

    • Refresh token: Required to access Google Drive. You can obtain one via the official website.
    • Automatically push changes: When enabled, the plugin schedules a push operation one minute after the most recent local file change.
    • Access token endpoint: The HTTPS URL used to exchange the refresh token for a Google access token. Defaults to https://ogd-server.richardxiong.com/api/access. You can self-host this service.
    • Client ID & Client secret: Optional OAuth credentials. If both are provided, the plugin exchanges refresh tokens directly with Google instead of using the access token endpoint.
  7. Manage Sync Lifecycle with ObsidianGoogleDrive

    master

    The ObsidianGoogleDrive class manages the synchronization state. Developers interacting with the plugin instance can use the following methods to control the sync process:

    • startSync(): Initiates a sync session. Checks for internet connectivity first. Throws an error if no connection is found. Sets syncing to true and updates the ribbon icon.
    • endSync(syncNotice?: Notice, retainConfigChanges?: boolean): Finalizes a sync session. Updates lastSyncedAt and changesToken. If retainConfigChanges is true, it ensures configuration files are synced. Returns true on success.
    • abortSync(syncNotice?: Notice): Stops the current sync attempt, clears the spinning ribbon icon, and resets the syncing state.
    • saveSettings(): Persists the current PluginSettings to disk.
    • loadSettings(): Loads settings from disk.
    // Example of manual sync control
    await plugin.startSync();
    // ... perform operations ...
    await plugin.endSync();
  8. Plugin Settings Schema

    master

    The following configuration keys are used by the plugin to manage state and synchronization:

    KeyTypeDescription
    refreshTokenstringThe token used to request new access tokens
    clientIdstringOptional OAuth client ID
    clientSecretstringOptional OAuth client secret
    accessTokenUrlstringThe endpoint for token exchange
    autoPushbooleanWhether to automatically push changes
    operationsRecord<string, 'create' | 'delete' | 'modify'>A map of file paths to their pending sync operations
    driveIdToPathRecord<string, string>Mapping of Google Drive IDs to local file paths
    rootFolderIdstringThe ID of the root folder in Google Drive
    lastSyncedAtnumberTimestamp of the last successful sync
    changesTokenstringThe token used to track changes in Google Drive
  9. Available Obsidian Commands

    master

    The plugin provides several commands to manage the synchronization between your local Obsidian vault and Google Drive:

    • Push to Google Drive: Manually uploads local changes to Google Drive.
    • Pull from Google Drive: Manually downloads changes from Google Drive to your local vault.
    • Reset local vault to Google Drive: Resets your local vault to match the state of Google Drive (use with caution).
    • Fix Google Drive paths: Attempts to correct pathing issues in Google Drive.