This repository has been archived by the owner on Oct 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #91: Bring in updated example scripts.
- Loading branch information
Showing
12 changed files
with
524 additions
and
402 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Backs up all sites on the target environment. | ||
* | ||
* Backs up db, theme, code, etc. | ||
* | ||
* Usage: | ||
* php backup.php live 'database,public files' | ||
* php backup.php live 'database,public files,private files' | ||
* php backup.php live | ||
*/ | ||
|
||
declare(strict_types = 1); | ||
|
||
use swichers\Acsf\Client\ClientFactory; | ||
use swichers\Acsf\Client\Endpoints\Entity\EntityInterface; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
require __DIR__ . '/utils.php'; | ||
|
||
// The environment to back up (dev, live, etc.) | ||
define('TARGET_ENV', $argv[1] ?? ''); | ||
// The comma separated list of components to backup. | ||
define('BACKUP_COMPONENTS', $argv[2] ?? 'database,public files,private files'); | ||
|
||
if (empty(TARGET_ENV)) { | ||
echo "Must supply an environment to back up.\n\n"; | ||
printf("Example: php %s live\n", basename(__FILE__)); | ||
die(1); | ||
} | ||
|
||
$start_time = new DateTime(); | ||
|
||
$client = ClientFactory::createFromEnvironment(TARGET_ENV); | ||
|
||
printf("Creating backups including %s\n", BACKUP_COMPONENTS); | ||
|
||
$client->getAction('Sites')->backupAll([ | ||
'components' => explode(',', BACKUP_COMPONENTS), | ||
], TRUE, 60, static function (EntityInterface $task, $taskStatus) use ($client) { | ||
$site_name = get_site_name($client, (int) $taskStatus['nid']); | ||
printf("Backup (%d, %s): %s\n", $task->id(), $site_name, $taskStatus['status_string']); | ||
}); | ||
|
||
$diff = $start_time->diff(new DateTime()); | ||
printf("Script complete. Time elapsed: %s\n", $diff->format('%H:%I:%S')); | ||
|
||
exit(0); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Deletes backups older than the given date. | ||
* | ||
* Usage: | ||
* php backups-prune.php dev 14 | ||
*/ | ||
|
||
declare(strict_types = 1); | ||
|
||
use swichers\Acsf\Client\ClientFactory; | ||
use swichers\Acsf\Client\ClientInterface; | ||
use swichers\Acsf\Client\Endpoints\Entity\Site; | ||
use Symfony\Component\HttpClient\Exception\ClientException; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
require __DIR__ . '/utils.php'; | ||
|
||
// The environment to remove backups from. | ||
define('TARGET_ENV', $argv[1] ?? ''); | ||
// The age of backups to remove. Backups older than this will be deleted. | ||
define('MAX_AGE_DAYS', (int) ($argv[2] ?? 14)); | ||
|
||
if (empty(TARGET_ENV)) { | ||
echo "Must supply an environment to back up.\n\n"; | ||
printf("Example: php %s live\n", basename(__FILE__)); | ||
die(1); | ||
} | ||
|
||
$start_time = new DateTime(); | ||
|
||
$client = ClientFactory::createFromEnvironment(TARGET_ENV); | ||
|
||
printf("Removing site backups older than %d days.\n", MAX_AGE_DAYS); | ||
|
||
/** @var \swichers\Acsf\Client\Endpoints\Entity\Site $site */ | ||
foreach ($client->getAction('Sites')->getAll() as $site) { | ||
prune_site_backups($client, $site, MAX_AGE_DAYS); | ||
} | ||
|
||
$diff = $start_time->diff(new DateTime()); | ||
printf("Script complete. Time elapsed: %s\n", $diff->format('%H:%I:%S')); | ||
|
||
exit(0); | ||
|
||
/** | ||
* Prune old backups for the given site. | ||
* | ||
* @param \swichers\Acsf\Client\ClientInterface $client | ||
* The ACSF client. | ||
* @param \swichers\Acsf\Client\Endpoints\Entity\Site $site | ||
* The Site to prune backups for. | ||
* @param int $maxAge | ||
* The age of backups to remove. Defaults to 2 weeks. | ||
* @param int|null $currentPage | ||
* The current page. | ||
* @param int $perPage | ||
* The amount of backups to list per page. | ||
* | ||
* @throws \swichers\Acsf\Client\Exceptions\InvalidOptionException | ||
*/ | ||
function prune_site_backups(ClientInterface $client, Site $site, int $maxAge = 14, int $currentPage = NULL, int $perPage = 20) { | ||
|
||
if (is_null($currentPage)) { | ||
// We need to calculate what the last page is so we can work our way | ||
// backwards. If we start at the front we may miss items as page numbers | ||
// shift during the loop. | ||
$currentPage = (int) ceil($site->listBackups(['limit' => 1])['count'] / $perPage) ?: 1; | ||
} | ||
|
||
try { | ||
$backups = $site->listBackups([ | ||
'limit' => $perPage, | ||
'page' => $currentPage, | ||
]); | ||
} | ||
catch (ClientException $x) { | ||
fwrite(STDERR, $x->getMessage() . PHP_EOL); | ||
return; | ||
} | ||
|
||
$prunables = array_filter($backups['backups'], static function ($backup) use ($maxAge) { | ||
return $backup['timestamp'] <= strtotime(sprintf('-%d days', $maxAge)); | ||
}); | ||
|
||
if (!empty($prunables)) { | ||
foreach ($prunables as $prunable) { | ||
printf("%s: Delete %s %s\n", $site->details()['site'], $prunable['label'], date('c', $prunable['timestamp'])); | ||
/** @var \swichers\Acsf\Client\Endpoints\Entity\Backup $backup */ | ||
$backup = $client->getEntity('Backup', $prunable['id'], $site); | ||
$backup->delete(); | ||
} | ||
} | ||
|
||
if ($currentPage > 1) { | ||
prune_site_backups($client, $site, $maxAge, $currentPage - 1, $perPage); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.