PHP code example of iobotis / php-incremental-backup

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

    

iobotis / php-incremental-backup example snippets


use Backup\Tools\Factory as ToolFactory;

$settings = array(
    'path_to_backup' => $path_to_backup,
    'destination' => array(
        'type' => 'local',
        'path' => '/path/to/save'
    ),
//    'passphrase' => 'abcdef',
//    'exclude' => array('folder')
);

$backup = ToolFactory::create('Duplicity', $settings);
$backup->execute();


use Backup\Tools\Factory as ToolFactory;
use Backup\IncrementalBackup;

use Backup\Tools\Factory as ToolFactory;

$settings = array(
    'path_to_backup' => $path_to_backup,
    'destination' => array(
        'type' => 'local',
        'path' => '/path/to/save'
    ),
//    'passphrase' => 'abcdef',
//    'exclude' => array('folder')
);

$backup = ToolFactory::create('Duplicity', $settings);
$backupClass = new IncrementalBackup ($backup);

$backups = $backupClass->getAllBackups();
foreach ($backups as $time) {
    echo 'There is a backup at ' . $time . "\n";
}

if( $backupClass->isChanged() ) {
    // back me up.
    echo 'Back up initiated' . "\n";
    $backupClass->createBackup();
}
else {
    echo 'No need to backup.' . "\n";
}

use Backup\Tools\Factory as ToolFactory;
use Backup\IncrementalBackup;

$settings = array(
    'path_to_backup' => $path_to_backup,
    'destination' => array(
        'type' => 'local',
        'path' => '/path/to/save'
    ),
//    'passphrase' => 'abcdef'
);

$duplicity = ToolFactory::create('Duplicity', $settings);
$backupClass = new IncrementalBackup ($duplicity);

// Restore last backup to this directory.
$backupClass->restoreTo(end( $backups ), '/path/to/restore');


use Backup\Tools\Factory as ToolFactory;
use Backup\IncrementalBackup;

$settings = array(
    'path_to_backup' => $path_to_backup,
    'destination' => array(
        'type' => 'local',
        'path' => $path_to_save
    ),
    //'exclude' => array('exclude', 'exclude1')
);

$backup = ToolFactory::create('Tar', $settings);
$backupClass = new IncrementalBackup ($backup);

$backups = $backupClass->getAllBackups();
foreach ($backups as $time) {
    echo 'There is a backup at ' . $time . "\n";
}

if( $backupClass->isChanged() ) {
    // back me up.
    echo 'Back up initiated' . "\n";
    $backupClass->createBackup();
}
else {
    echo 'No need to backup.' . "\n";
}


use Backup\Tools\Factory as ToolFactory;
use Backup\IncrementalBackup;

$settings = array(
    'path_to_backup' => $path_to_backup,
    'destination' => array(
        'type' => 'local',
        'path' => $path_to_save
    ),
);

$backup = ToolFactory::create('Tar', $settings);
$backupClass = new IncrementalBackup ( $backup );

$backups = $backupClass->getAllBackups();

// Restore last backup to this directory.
$backupClass->restoreTo( end( $backups ), '/path/to/restore' );


use Backup\Binary;
use Backup\FileSystem\Source;
use Backup\Destination\Factory as DesFactory;
use Backup\Tools\Duplicity;
use Backup\FileSystem\Folder;

$binary = new Binary('/usr/bin/duplicity');
$source = new Source('/var/www/example_com');
$destination = DesFactory::create('/var/backups/example_com');

$duplicity = new Duplicity($source,$destination,$binary);

$duplicity->setArchiveDir('/var/www/cache');
$duplicity->setExludedSubDirectories(array('cache', 'logs', 'tmp'));

// check if duplicity is installed.
$duplicity->isInstalled();

// get duplicity version.
$duplicity->getVersion();

// verify backup location.
$duplicity->verify();

// backup if needed.
$duplicity->execute();

// retrieve existing backups.
$backups = $duplicity->getAllBackups();

// restore 1st backup.
$folder = new Folder('/var/www/example_com');
$duplicity->restore($backups[0], $folder);