PHP code example of sebastiansulinski / php-backup

1. Go to this page and download the library: Download sebastiansulinski/php-backup 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/ */

    

sebastiansulinski / php-backup example snippets




use SSD\DotEnv\DotEnv;
use SSD\Backup\Backup;
use SSD\Backup\Jobs\File;
use SSD\Backup\Jobs\Directory;
use SSD\Backup\Remotes\Dropbox;
use SSD\Backup\Jobs\MySQLDatabase;

use Carbon\Carbon;
use Illuminate\Filesystem\Filesystem;
use Maknz\Slack\Client as SlackClient;

$dotenv = new DotEnv([__DIR__ . '/.env']);
$dotenv->load();
$dotenv->slack_channel',
    'link_names' => true
]);

$client->send('Project backup started at: ' . Carbon::now()->toDateTimeString());

try {

    $remote = new Dropbox(
        getenv('DROPBOX_OAUTH')
    );

    $backup = new Backup(
        $remote,
        $workingDirectory
    );

    // directory to which backup should be saved on the remote server
    $backup->setRemoteDirectory(getenv('REMOTE_DIR_NAME'));

    // keep only 7 backups then overwrite the oldest one
    $backup->setNumberOfBackups(7);

    // add MySQL database to the backup
    $backup->addJob(new Job(
        new MySQLDatabase([
            'host' => getenv('DB_HOST'),
            'name' => getenv('DB_NAME'),
            'user' => getenv('DB_USER'),
            'password' => getenv('DB_PASS')
        ]),
        'database'
    ));

    // add single file to the backup
    $backup->addJob(new Job(
        new File(
            __DIR__ . '/files/text.txt',
            __DIR__
        ),
        'files'
    ));

    // add the 'files' directory to the backup
    // but exclude the 'css' directory within
    $backup->addJob(new Job(
        new Directory(
            __DIR__ . '/files',
            __DIR__,
            [
                'files/css'
            ]
        ),
        'files'
    ));

    // run backup
    $backup->run();

} catch (Exception $exception) {

    $client->send('Project backup failed at: ' . Carbon::now()->toDateTimeString() .' with message: "'.$exception->getMessage().'"');

    $filesystem = new Filesystem;
    
    $filesystem->cleanDirectory($workingDirectory);

    $filesystem->prepend(
        $workingDirectory . DIRECTORY_SEPARATOR . 'error_log',
        $exception->getMessage() . PHP_EOL
    );

} finally {
 
    $client->send('Project backup finished at: ' . Carbon::now()->toDateTimeString());

}



use SSD\DotEnv\DotEnv;
use SSD\Backup\Backup;
use SSD\Backup\Jobs\File;
use SSD\Backup\Remotes\Ftp;
use SSD\Backup\Jobs\Directory;
use SSD\Backup\Jobs\MySQLDatabase;

use Illuminate\Filesystem\Filesystem;

try {

    $dotenv = new DotEnv([
        __DIR__ . '/.env'
    ]);
    $dotenv->load();
    $dotenv->'),
        getenv('FTP_USER'),
        getenv('FTP_PASS')
    );

    $backup = new Backup(
        $remote,
        $workingDirectory
    );

    // directory to which backup should be saved on the remote server
    $backup->setRemoteDirectory(getenv('REMOTE_DIR_NAME'));

    // keep only 7 backups then overwrite the oldest one
    $backup->setNumberOfBackups(7);

    // add MySQL database to the backup
    $backup->addJob(new Job(
        new MySQLDatabase([
            'host' => getenv('DB_HOST'),
            'name' => getenv('DB_NAME'),
            'user' => getenv('DB_USER'),
            'password' => getenv('DB_PASS')
        ]),
        'database'
    ));

    // add single file to the backup
    $backup->addJob(new Job(
        new File(
            __DIR__ . '/files/text.txt',
            __DIR__
        ),
        'files'
    ));

    // add the entire directory to the backup
    $backup->addJob(new Job(
        new Directory(
            __DIR__ . '/files/css',
            __DIR__ . '/files'
        ),
        'files'
    ));

    // run backup
    $backup->run();

} catch (Exception $e) {

    $filesystem = new Filesystem;
    
    $filesystem->cleanDirectory($workingDirectory);

    $filesystem->prepend(
        $workingDirectory . DIRECTORY_SEPARATOR . 'error_log',
        $e->getMessage() . PHP_EOL
    );

}