PHP code example of innoge / laravel-rclone

1. Go to this page and download the library: Download innoge/laravel-rclone library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

innoge / laravel-rclone example snippets


use InnoGE\LaravelRclone\Facades\Rclone;

// Simple sync
Rclone::source('s3', 'documents')
    ->target('backup', 'archive')
    ->sync();

// With progress & options
Rclone::source('s3', 'media/photos')
    ->target('local', 'storage/backups')
    ->withProgress()
    ->transfers(16)
    ->checkers(8)
    ->getOutputUsing(fn($type, $output) => Log::info("Rclone: {$output}"))
    ->sync();

// config/filesystems.php
'disks' => [
    's3' => [
        'driver' => 's3',
        'key' => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
        'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
        'bucket' => env('AWS_BUCKET'),
    ],
    'sftp' => [
        'driver' => 'sftp',
        'host' => env('SFTP_HOST'),
        'username' => env('SFTP_USERNAME'),
        'password' => env('SFTP_PASSWORD'),
        'port' => env('SFTP_PORT', 22),
    ],
]

// Sync (make target identical to source)
$result = Rclone::source('s3', 'data')->target('backup')->sync();

// Copy (don't delete from target)
$result = Rclone::source('s3', 'data')->target('backup')->copy();

// Move (delete from source after transfer)
$result = Rclone::source('s3', 'data')->target('backup')->move();

Rclone::source('s3', 'large-dataset')
    ->target('backup', 'archive')
    ->transfers(32)          // Parallel transfers
    ->checkers(16)           // File checkers
    ->retries(5)             // Retry attempts
    ->statInterval(10)       // Stats interval (seconds)
    ->option('bandwidth', '50M')  // Custom rclone option
    ->sync();

$result = Rclone::source('s3', 'files')
    ->target('local', 'backup')
    ->withProgress()
    ->getOutputUsing(function ($type, $output) {
        if ($type === 'out') {
            echo "Progress: {$output}";
        } else {
            Log::error("Rclone Error: {$output}");
        }
    })
    ->sync();

// Check results
if ($result->isSuccessful()) {
    $stats = $result->getStats();
    echo "Transferred: {$stats['transferred_files']} files\n";
    echo "Data: {$stats['transferred_bytes']} bytes\n";
} else {
    echo "Failed: {$result->getErrorOutput()}\n";
}

's3' => [
    'driver' => 's3',
    'key' => 'AKIAIOSFODNN7EXAMPLE',
    'secret' => 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY',
    'region' => 'us-west-2',
    'bucket' => 'my-bucket',
    'endpoint' => 'https://s3.amazonaws.com', // Optional
    'use_path_style_endpoint' => false,        // Optional
]

'sftp' => [
    'driver' => 'sftp',
    'host' => 'server.example.com',
    'username' => 'user',
    // Password auth
    'password' => 'secret',
    // OR Key file auth
    'key_file' => '/path/to/private/key',
    // OR Inline key auth
    'private_key' => '-----BEGIN OPENSSH PRIVATE KEY-----...',
    'port' => 22,
]

use InnoGE\LaravelRclone\Contracts\RcloneInterface;

class BackupService
{
    public function __construct(
        private RcloneInterface $rclone
    ) {}

    public function dailyBackup(): bool
    {
        $result = $this->rclone
            ->source('local', 'app/data')
            ->target('s3', 'backups/daily')
            ->withProgress()
            ->sync();

        return $result->isSuccessful();
    }
}

use InnoGE\LaravelRclone\Facades\Rclone;

class BackupCommand extends Command
{
    public function handle(): int
    {
        $this->info('Starting backup...');

        $result = Rclone::source('local', 'storage/app')
            ->target('s3', 'backups/' . now()->format('Y-m-d'))
            ->withProgress()
            ->getOutputUsing(fn($type, $output) => $this->line($output))
            ->sync();

        return $result->isSuccessful() ? 0 : 1;
    }
}

return [
    'binary_path' => env('RCLONE_BINARY_PATH', 'rclone'),
    'timeout' => env('RCLONE_TIMEOUT', 3600),
    'base_options' => [
        '--delete-after',
        '--fast-list',
        '--checksum',
    ],
    'defaults' => [
        'transfers' => 4,
        'checkers' => 8,
        'retries' => 3,
        'progress' => false,
    ],
];
bash
php artisan vendor:publish --provider="InnoGE\LaravelRclone\RcloneServiceProvider" --tag="rclone-config"