如何实现Google Cloud SSH终端与本地Linux存储的文件同步共享?
Absolutely! You can set up bidirectional file sync between your local Linux machine and your Google Cloud SSH terminal—no more tedious manual deleting, uploading, or restarting sessions just to update your Discord bot or database files. Here are the most practical, user-friendly methods tailored to your use case:
1. Real-Time Bidirectional Sync with unison
unison is a dedicated bidirectional file sync tool that’s lightweight and perfect for keeping directories in sync across local and remote systems. It handles conflicts gracefully and works over SSH.
Setup Steps:
- Install
unisonon both systems:
On your local Linux machine:
In your Google Cloud SSH terminal:sudo apt install unison # For Debian/Ubuntu-based distros # Use dnf install unison for Fedora/RHELsudo apt install unison - Enable passwordless SSH login (so sync runs without prompting for passwords):
Generate an SSH key locally if you don’t have one:
Copy your public key to the Google Cloud instance:ssh-keygen -t ed25519ssh-copy-id your-gcloud-username@your-instance-external-ip - Run the sync command:
To sync your local bot folder with the remote one (and vice versa):unison /path/to/local/discord-bot ssh://your-gcloud-username@your-instance-external-ip//path/to/remote/discord-bot -auto -batch-auto: Automatically accepts default sync actions (great for non-interactive use)-batch: Runs without user prompts
- Set up real-time sync:
For instant sync when files change, add the-repeat watchflag:unison /path/to/local/discord-bot ssh://your-gcloud-username@your-instance-external-ip//path/to/remote/discord-bot -auto -batch -repeat watch
Pros & Cons:
- ✅ Lightweight, no extra services needed
- ✅ Handles file permissions and deletions properly
- ❌ Requires manual conflict resolution if both sides edit the same file simultaneously (low risk if you’re the only user)
2. Mount Remote GCloud Directory Locally with sshfs
This method lets you treat the Google Cloud instance’s file system like a local folder—edit files directly on your machine, and changes sync instantly to the cloud (and vice versa).
Setup Steps:
- Install
sshfslocally:sudo apt install sshfs # Debian/Ubuntu # Use dnf install sshfs for Fedora/RHEL - Create a local mount point:
mkdir ~/gcloud-discord-bot - Mount the remote directory:
sshfs your-gcloud-username@your-instance-external-ip:/path/to/remote/discord-bot ~/gcloud-discord-bot - Start editing!
Open files in~/gcloud-discord-botwith your favorite local editor—every change is immediately reflected on the Google Cloud instance. To unmount when done:fusermount -u ~/gcloud-discord-bot
Pros & Cons:
- ✅ Most intuitive workflow (feels exactly like editing local files)
- ✅ Real-time sync without extra commands
- ❌ Relies on a stable SSH connection (may disconnect if your internet drops)
3. Version-Controlled Sync with Git
If you want to track changes to your bot code (and roll back if needed), Git is a perfect fit. It doubles as a sync tool while keeping a full history of your edits.
Setup Steps:
- Initialize Git locally:
cd /path/to/local/discord-bot git init git add . git commit -m "Initial bot code commit" - Create a bare Git repo on the cloud:
In your Google Cloud SSH terminal:mkdir -p ~/bot-repo.git cd ~/bot-repo.git git init --bare - Link local repo to the cloud:
Back on your local machine:git remote add gcloud ssh://your-gcloud-username@your-instance-external-ip/home/your-gcloud-username/bot-repo.git git push gcloud main - Pull code to the cloud’s working directory:
In the SSH terminal:cd /path/to/remote/discord-bot git init git remote add origin ~/bot-repo.git git pull origin main - Automate updates:
Add apost-receivehook to the cloud’s bare repo so pushes from local automatically update the working directory:
Paste this content:nano ~/bot-repo.git/hooks/post-receive
Make the hook executable:#!/bin/bash git --work-tree=/path/to/remote/discord-bot --git-dir=/home/your-gcloud-username/bot-repo.git checkout -fchmod +x ~/bot-repo.git/hooks/post-receive
Pros & Cons:
- ✅ Full version history and rollback capabilities
- ✅ Great for collaboration (if others work on the bot)
- ❌ Less ideal for large database files (Git tracks entire file changes, not incremental edits)
Important Note for Database Files
For file-based databases like SQLite, avoid syncing the live .db file directly—this can cause corruption if the bot is accessing the file during sync. Instead:
- Stop the bot temporarily on the cloud:
pkill -f your-bot-file.py - Export the database to a SQL file:
sqlite3 your-db.db .dump > db-backup.sql - Sync the SQL file using one of the methods above
- Import the SQL file on the cloud:
sqlite3 your-db.db < db-backup.sql - Restart the bot:
python3 your-bot-file.py &
内容的提问来源于stack exchange,提问作者RobinR




