You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何编写脚本实现Windows/Linux环境下特定文件夹文件创建后自动调用rclone迁移文件至远程文件夹

Got it, let's walk through how to set up folder monitoring and automatic rclone transfers on both Windows and Linux—since rclone doesn't have built-in watch functionality, we'll use native tools to get the job done.

Windows Solution (PowerShell)

Windows has a built-in FileSystemWatcher class in .NET which we can leverage directly in PowerShell. This script will monitor your target folder, wait for new files to finish writing, then trigger an rclone move to your remote.

Step 1: Write the PowerShell Script

Save this as rclone_folder_watcher.ps1:

# Configure your paths here
$watchFolder = "C:\Path\To\Your\Local\Folder"
$remoteDestination = "your_remote_name:/path/to/remote/folder"
$logPath = "C:\Logs\rclone_watch.log"

# Create the file system watcher
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = $watchFolder
$watcher.IncludeSubdirectories = $false  # Set to $true if you need to watch subfolders
$watcher.Filter = "*.*"  # Monitor all file types; adjust if needed
$watcher.EnableRaisingEvents = $true

# Define action to run when a new file is created
$onFileCreated = {
    $filePath = $Event.SourceEventArgs.FullPath
    $fileName = $Event.SourceEventArgs.Name
    $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
    
    # Log the event
    Add-Content $logPath -Value "$timestamp - Detected new file: $fileName"

    # Wait for the file to be fully written (avoids transferring incomplete files)
    while ($true) {
        try {
            $fileStream = [System.IO.File]::Open($filePath, 'Open', 'ReadWrite', 'None')
            $fileStream.Close()
            break
        }
        catch {
            Start-Sleep -Milliseconds 500
        }
    }

    # Execute rclone move (use 'copy' instead if you want to keep the local file)
    rclone move "$filePath" "$remoteDestination" --log-file="$logPath" -v
}

# Register the event handler
Register-ObjectEvent $watcher "Created" -Action $onFileCreated

# Keep the script running
Write-Host "Monitoring $watchFolder for new files. Press Ctrl+C to stop."
while ($true) { Start-Sleep 1 }

Step 2: Configure & Run

  1. First, make sure rclone is set up with your remote: run rclone config in PowerShell to add and authenticate your remote storage.
  2. Update the $watchFolder, $remoteDestination, and $logPath variables in the script to match your setup.
  3. Run the script: Open PowerShell, navigate to the script's folder, and run .\rclone_folder_watcher.ps1.
  4. To make it run on startup: Use Windows Task Scheduler to create a task that runs this script when you log in (set the action to start powershell.exe with argument -File "C:\Path\To\rclone_folder_watcher.ps1").

Linux Solution (inotify-tools + Bash)

Linux uses the inotify kernel interface for file system events. We'll use inotifywait (from the inotify-tools package) to monitor the folder, then a bash script to handle the rclone transfer.

Step 1: Install inotify-tools

  • For Debian/Ubuntu-based systems:
    sudo apt update && sudo apt install inotify-tools -y
    
  • For RHEL/CentOS/Fedora:
    sudo dnf install inotify-tools -y
    

Step 2: Write the Bash Script

Save this as rclone_folder_watcher.sh:

#!/bin/bash

# Configure your paths here
WATCH_FOLDER="/path/to/your/local/folder"
REMOTE_DEST="your_remote_name:/path/to/remote/folder"
LOG_FILE="/var/log/rclone_watch.log"

# Create log file if it doesn't exist
mkdir -p $(dirname $LOG_FILE)
touch $LOG_FILE

# Function to handle new file transfers
transfer_file() {
    local file="$1"
    local timestamp=$(date '+%Y-%m-%d %H:%M:%S')

    # Wait for the file to be fully written (skip if it's a directory)
    if [ -f "$file" ]; then
        while lsof "$file" >/dev/null 2>&1; do
            sleep 0.5
        done

        echo "$timestamp - Starting transfer of: $file" >> $LOG_FILE
        # Use 'rclone copy' instead of 'move' if you want to retain the local file
        rclone move "$file" "$REMOTE_DEST" --log-file="$LOG_FILE" -v
        echo "$timestamp - Transfer completed for: $file" >> $LOG_FILE
    fi
}

# Start monitoring the folder for new files
echo "$(date '+%Y-%m-%d %H:%M:%S') - Starting folder watcher on $WATCH_FOLDER" >> $LOG_FILE
inotifywait -m -e create --format "%w%f" "$WATCH_FOLDER" | while read -r new_item; do
    transfer_file "$new_item" &
done

Step 3: Configure & Run

  1. Ensure rclone is configured: Run rclone config to set up your remote (the config file lives in ~/.config/rclone/rclone.conf).
  2. Make the script executable:
    chmod +x rclone_folder_watcher.sh
    
  3. Test run:
    ./rclone_folder_watcher.sh
    
  4. Run as a background service (systemd):
    Create a systemd service file at /etc/systemd/system/rclone-watcher.service:
    [Unit]
    Description=Rclone Folder Watcher Service
    After=network.target
    
    [Service]
    User=your_username
    ExecStart=/path/to/rclone_folder_watcher.sh
    Restart=always
    RestartSec=5
    
    [Install]
    WantedBy=multi-user.target
    
    Then enable and start the service:
    sudo systemctl daemon-reload
    sudo systemctl enable rclone-watcher.service
    sudo systemctl start rclone-watcher.service
    

Key Notes for Both Systems

  • File Lock Handling: Both scripts include logic to wait for files to be fully written before transferring—this prevents incomplete files from being sent to the remote.
  • Logging: Logs are written to a specified file to help troubleshoot transfer issues.
  • rclone Commands: Use rclone copy instead of move if you want to keep the local file after transfer. Add flags like --progress if you want to see transfer details in real-time.

内容的提问来源于stack exchange,提问作者dawnslayer

火山引擎 最新活动