Skip to content

Commit

Permalink
Issue #6: Updated sync tasks to use rsync for the files.
Browse files Browse the repository at this point in the history
  • Loading branch information
matthijs-va committed Aug 17, 2017
1 parent 550236a commit f89cc88
Showing 1 changed file with 181 additions and 90 deletions.
271 changes: 181 additions & 90 deletions src/AbstractRoboFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
use DigipolisGent\Robo\Task\Deploy\Ssh\Auth\AbstractAuth;
use DigipolisGent\Robo\Task\Deploy\Ssh\Auth\KeyFile;
use DigipolisGent\Robo\Task\General\Common\DigipolisPropertiesAwareInterface;
use function ltrim;
use function md5;
use Robo\Contract\ConfigAwareInterface;
use Robo\Task\Filesystem\FilesystemStack;
use Symfony\Component\Finder\Finder;
use function uniqid;

abstract class AbstractRoboFile extends \Robo\Tasks implements DigipolisPropertiesAwareInterface, ConfigAwareInterface
{
Expand Down Expand Up @@ -471,70 +474,139 @@ protected function syncTask(
$destinationAuth = new KeyFile($destinationUser, $destinationKeyFile);

$collection = $this->collectionBuilder();
// Create a backup.
$collection->addTask(
$this->backupTask(
$sourceHost,
$sourceAuth,
$sourceRemote,
$opts
)
);
// Download the backup.
$collection->addTask(
$this->downloadBackupTask(
$sourceHost,
$sourceAuth,
$sourceRemote,
$opts
)
);
// Remove the backup from the source.
$collection->addTask(
$this->removeBackupTask(
$sourceHost,
$sourceAuth,
$sourceRemote,
$opts
)
);
// Upload the backup.
$collection->addTask(
$this->uploadBackupTask(
$destinationHost,
$destinationAuth,
$destinationRemote,
$opts
)
);
// Restore the backup.
$collection->addTask(
$this->restoreBackupTask(
$destinationHost,
$destinationAuth,
$destinationRemote,
$opts
)
);
// Remove the backup from the destination.
$collection->completion(
$this->removeBackupTask(
$destinationHost,
$destinationAuth,
$destinationRemote,
$opts
)
);
// Finally remove the local backups.

$dbBackupFile = $this->backupFileName('.sql.gz', $sourceRemote['time']);
$filesBackupFile = $this->backupFileName('.tar.gz', $sourceRemote['time']);
if ($opts['files']) {
$temp_key = '~/.ssh/' . uniqid('robo_', TRUE) . '.id_rsa';

// Generate a temporary key.
$collection->addTask(
$this->taskExec('ssh-keygen -q -t rsa -b 4096 -N "" -f ' . $temp_key . ' -C "robo:' . md5($temp_key) . '"')
);

$collection->completion(
$this->taskExecStack()
->exec('rm -f ' . $temp_key . '*')
);

// Install it on the destination host.
$collection->addTask(
$this->taskExec('cat ' . $temp_key . '.pub | ssh ' . $destinationUser . '@' . $destinationHost . ' -o StrictHostKeyChecking=no -i ' . $destinationKeyFile . ' "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"')
);

$collection->completion(
$this->taskSsh($destinationHost, $destinationAuth)
->exec('sed -i "/robo:' . md5($temp_key) . '/d" ~/.ssh/authorized_keys')
);

$rsync = $this->taskRsync()
->fromPath($temp_key)
->toHost($sourceHost)
->toUser($sourceUser)
->toPath('~/.ssh')
->compress()
->archive()
->checksum()
->wholeFile();

$rsync->rawArg('--rsh "ssh -o StrictHostKeyChecking=no -i ' . $sourceKeyFile . '"');

$collection->addTask($rsync);

$collection->completion(
$this->taskSsh($sourceHost, $sourceAuth)
->exec('rm -f ' . $temp_key)
);

$rsync = $this->taskRsync()
->fromPath($sourceRemote['filesdir'])
->toHost($destinationHost)
->toUser($destinationUser)
->toPath($destinationRemote['filesdir'])
->recursive()
->delete()
->compress()
->checksum()
->wholeFile();

$rsync->rawArg('--rsh "ssh -o StrictHostKeyChecking=no -i $ROBO_KEY"');

foreach ($this->excludeFromBackup as $exclude) {
$rsync->exclude($exclude);
}

$collection->addTask(
$this->taskSsh($sourceHost, $sourceAuth)
->exec('ROBO_KEY=`readlink -e ' . $temp_key . '` && export ROBO_KEY && '. $rsync->getCommand())
);
}

if ($opts['data']) {
$opts['files'] = FALSE;

// Create a backup.
$collection->addTask(
$this->backupTask(
$sourceHost,
$sourceAuth,
$sourceRemote,
$opts
)
);
// Sync the backup.
$collection->addTask(
$this->downloadBackupTask(
$sourceHost,
$sourceAuth,
$sourceRemote,
$opts
)
);
// Remove the backup from the source.
$collection->addTask(
$this->removeBackupTask(
$sourceHost,
$sourceAuth,
$sourceRemote,
$opts
)
);
// Upload the backup.
$collection->addTask(
$this->uploadBackupTask(
$destinationHost,
$destinationAuth,
$destinationRemote,
$opts
)
);
// Restore the backup.
$collection->addTask(
$this->restoreBackupTask(
$destinationHost,
$destinationAuth,
$destinationRemote,
$opts
)
);
// Remove the backup from the destination.
$collection->completion(
$this->removeBackupTask(
$destinationHost,
$destinationAuth,
$destinationRemote,
$opts
)
);

// Finally remove the local backups.
$dbBackupFile = $this->backupFileName('.sql.gz', $sourceRemote['time']);

$collection->completion(
$this->taskExecStack()
->exec('rm -rf ' . $dbBackupFile)
);
}

$collection->completion(
$this->taskExecStack()
->exec('rm -rf ' . $dbBackupFile)
->exec('rm -rf ' . $filesBackupFile)
);
return $collection;
}

Expand Down Expand Up @@ -714,12 +786,13 @@ protected function preRestoreBackupTask(
foreach ($this->fileBackupSubDirs as $subdir) {
$removeFiles .= ' ' . $subdir . '/* ' . $subdir . '/.??*';
}
$collection = $this->collectionBuilder();
$collection->taskSsh($worker, $auth)

return $this->taskSsh($worker, $auth)
->remoteDirectory($remote['filesdir'], true)
->exec($removeFiles);
}
return $collection;

return FALSE;
}

/**
Expand Down Expand Up @@ -993,45 +1066,63 @@ public function digipolisSyncLocal(
$local = $this->getLocalSettings($opts['app']);
$auth = new KeyFile($user, $keyFile);
$collection = $this->collectionBuilder();
// Create a backup.
$collection->addTask(
$this->backupTask(
$host,
$auth,
$remote,
$opts
)
);
// Download the backup.
$collection->addTask(
$this->downloadBackupTask(
$host,
$auth,
$remote,
$opts
)
);

$collection->taskExecStack();
if ($opts['files']) {
$filesBackupFile = $this->backupFileName('.tar.gz', $remote['time']);
$collection
->taskExecStack()
->exec('chown -R $USER ' . dirname($local['filesdir']))
->exec('chmod -R u+w ' . dirname($local['filesdir']))
->exec('rm -rf ' . $local['filesdir'] . '/* ' . $local['filesdir'] . '/.??*')
->exec('tar -xkzf ' . $filesBackupFile . ' -C ' . $local['filesdir'])
->exec('rm -rf ' . $filesBackupFile);
->exec('chmod -R u+w ' . dirname($local['filesdir']));

$rsync = $this->taskRsync()
->fromHost($host)
->fromUser($user)
->fromPath($remote['filesdir'])
->toPath($local['filesdir'])
->recursive()
->delete()
->compress()
->checksum()
->wholeFile();

$rsync->rawArg('--rsh "ssh -o StrictHostKeyChecking=no -i ' . $keyFile . '"');

$collection->addTask($rsync);
}

// Restore the db backup.

if ($opts['data']) {
$opts['files'] = FALSE;

// Create a backup.
$collection->addTask(
$this->backupTask(
$host,
$auth,
$remote,
$opts
)
);
// Download the backup.
$collection->addTask(
$this->downloadBackupTask(
$host,
$auth,
$remote,
$opts
)
);

// Restore the db backup.
$dbBackupFile = $this->backupFileName('.sql.gz', $remote['time']);
$dbRestore = 'vendor/bin/robo digipolis:database-restore '
. '--source=' . $dbBackupFile;
$cwd = getcwd();

$collection->taskExecStack();
$collection->exec('cd ' . $this->getConfig()->get('digipolis.root.project') . ' && ' . $dbRestore);
$collection->exec('cd ' . $cwd . ' && rm -rf ' . $dbBackupFile);
}

return $collection;
}

Expand Down

0 comments on commit f89cc88

Please sign in to comment.