Skip to content

Instantly share code, notes, and snippets.

@vikramsoni2
Last active April 10, 2025 17:03
Show Gist options
  • Save vikramsoni2/9e4191c9ecdfae59f56685b03e8c048a to your computer and use it in GitHub Desktop.
Save vikramsoni2/9e4191c9ecdfae59f56685b03e8c048a to your computer and use it in GitHub Desktop.
WSL projects folder backup to onedrive
# Sync configuration
SOURCE_DIR="/home/ubuntu/projects"
DEST_DIR="/mnt/c/Users/vsoni/OneDrive - Path"
# Exclude patterns (matching any folder name or path)
EXCLUDE_PATTERNS=(
"baxicore"
"node_modules"
".git"
"venv"
"*.log"
"*.tmp"
"openwebui"
"baxi-backend-experts"
"document_processing"
"baxi-frontend"
"myenv"
".venv"
)
# Optional cron schedule example (for your reference)
# CRON_SCHEDULE="0 */2 * * *" # Every 2 hours

WSL Project Synchronization Tool

This tool provides an automated way to synchronize projects between your WSL (Windows Subsystem for Linux) environment and Windows file system. It's particularly useful for backing up your WSL projects to Windows or for accessing your WSL files through Windows applications.

Overview

The synchronization tool uses rsync to efficiently copy files from a source directory (typically in WSL) to a destination directory (typically in Windows), while respecting exclusion patterns for files and directories you don't want to sync.

Features

  • One-way synchronization from WSL to Windows
  • Configurable source and destination directories
  • Customizable file/directory exclusion patterns
  • Optional automated syncing via cron jobs
  • Logging of sync operations

Prerequisites

  • WSL installed on Windows
  • rsync installed in your WSL distribution
  • Basic knowledge of bash scripting

Installation

  1. Create the configuration file:

    touch ~/.syncservice.conf
  2. Save the sync_projects.sh script to a location of your choice (e.g., ~/scripts/sync_projects.sh)

  3. Make the script executable:

    chmod +x ~/scripts/sync_projects.sh

Configuration

Setting up the configuration file

Edit the ~/.syncservice.conf file with your preferred text editor:

nano ~/.syncservice.conf

Add the following content, adjusting paths and exclusion patterns to your needs:

# Sync configuration
SOURCE_DIR="/home/your-username/projects"
DEST_DIR="/mnt/c/Users/your-windows-username/Documents/projects"

# Exclude patterns (matching any folder name or path)
EXCLUDE_PATTERNS=(
  "node_modules"
  ".git"
  "venv"
  "*.log"
  "*.tmp"
  # Add any other directories or file patterns you want to exclude
)

Configuration Options

  • SOURCE_DIR: The WSL directory containing your projects
  • DEST_DIR: The Windows directory where files will be synchronized to
  • EXCLUDE_PATTERNS: An array of file patterns or directory names to exclude from synchronization

Usage

Manual Synchronization

To run a sync operation manually:

~/scripts/sync_projects.sh --sync

Automated Synchronization

When you run the script without the --sync flag in an interactive terminal, it will offer to set up automated synchronization via cron:

~/scripts/sync_projects.sh

Follow the prompts to select your preferred sync frequency:

  1. Every hour
  2. Every 2 hours
  3. Every 6 hours
  4. Daily
  5. Custom cron schedule
  6. Cancel

The script will add an appropriate cron job to run synchronization automatically.

Running in Non-Interactive Mode

To run the sync operation without interactive prompts (e.g., from another script):

~/scripts/sync_projects.sh --sync

Logs

Sync operations are logged to ~/sync.log when run via cron. You can check this file to verify that syncs are occurring as expected:

tail -f ~/sync.log

Customizing Exclusions

The default configuration excludes common directories and files you typically don't want to synchronize, such as:

  • Node.js modules (node_modules)
  • Git repositories (.git)
  • Python virtual environments (venv, .venv, myenv)
  • Log files (*.log)
  • Temporary files (*.tmp)

You can customize these exclusions in the ~/.syncservice.conf file.

Troubleshooting

Sync Fails

If synchronization fails, check:

  1. That both source and destination directories exist
  2. That you have appropriate permissions
  3. That there's enough disk space in the destination
  4. The sync log file for specific error messages

Cron Job Not Running

If the automated sync isn't running:

  1. Check if the cron job was installed correctly:
    crontab -l
  2. Verify that the cron service is running:
    service cron status
  3. Check the log file for errors:
    cat ~/sync.log

Security Considerations

  • The script preserves file content but does not preserve ownership or group information
  • Consider the security implications of syncing sensitive files to Windows

Limitations

  • This is a one-way sync from WSL to Windows
  • Large repositories with many files may take time to sync initially
  • Windows file system limitations may affect certain Linux files

Uninstalling

To remove the automated sync:

crontab -l | grep -v "SYNC_PROJECTS_CRON" | crontab -

To completely remove the tool, also delete:

  • The script file
  • The configuration file (~/.syncservice.conf)
  • The log file (~/sync.log)
#!/bin/bash
# Configuration
CONFIG_FILE="$HOME/.syncservice.conf"
LOG_FILE="$HOME/sync.log"
CRON_COMMENT="# SYNC_PROJECTS_CRON"
# Load configuration
load_config() {
if [ ! -f "$CONFIG_FILE" ]; then
echo "Error: Configuration file $CONFIG_FILE not found" >&2
exit 1
fi
source "$CONFIG_FILE"
}
# Main sync function
run_sync() {
# Validate directories
if [ -z "$SOURCE_DIR" ] || [ -z "$DEST_DIR" ]; then
echo "Error: Source/Destination directories not configured" >&2
exit 1
fi
if [ ! -d "$SOURCE_DIR" ]; then
echo "Error: Source directory $SOURCE_DIR does not exist" >&2
exit 1
fi
# Prepare rsync command
rsync_args=(
-av
--no-owner --no-group
--delete
)
# Add exclude patterns
for pattern in "${EXCLUDE_PATTERNS[@]}"; do
rsync_args+=(--exclude="$pattern")
done
rsync_args+=("${SOURCE_DIR%/}/" "$DEST_DIR")
# Create destination directory if needed
mkdir -p "$DEST_DIR"
# Execute sync
echo "Starting sync: $(date)"
if rsync "${rsync_args[@]}"; then
echo "Sync completed successfully"
else
echo "Sync failed with error code $?" >&2
exit 1
fi
}
# Interactive cron setup
setup_cron() {
echo "Would you like to set up automatic syncing? (yes/no)"
read -r answer
if [ "$answer" != "yes" ]; then
echo "Skipping cron setup"
return
fi
# Check for existing cron job
if crontab -l | grep -qF "$CRON_COMMENT"; then
echo "Cron job already exists. Skipping setup."
return
fi
# Get schedule selection
echo "Select sync frequency:"
echo "1) Every hour"
echo "2) Every 2 hours"
echo "3) Every 6 hours"
echo "4) Daily"
echo "5) Custom cron schedule"
echo "6) Cancel"
read -r -p "Enter choice (1-6): " choice
case $choice in
1) schedule="0 * * * *" ;;
2) schedule="0 */2 * * *" ;;
3) schedule="0 */6 * * *" ;;
4) schedule="0 0 * * *" ;;
5)
read -r -p "Enter cron schedule (e.g., '0 */4 * * *'): " schedule
;;
6)
echo "Cron setup cancelled"
return
;;
*)
echo "Invalid choice"
return
;;
esac
# Get absolute path to script
script_path=$(realpath "$0")
# Add to crontab
(
crontab -l 2>/dev/null
echo "$CRON_COMMENT"
echo "$schedule $script_path --sync >> $LOG_FILE 2>&1"
) | crontab -
echo "Cron job installed successfully!"
echo "Syncing will run on schedule: $schedule"
}
# Execution flow
if [ "$1" = "--sync" ]; then
load_config
run_sync
else
load_config
# Interactive mode
if [ -t 0 ]; then
setup_cron
echo -e "\nRunning initial sync..."
fi
run_sync
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment