Skip to content

Commit

Permalink
Roll versions back to ss3 compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
Danae Miller-Clendon committed Jun 19, 2019
1 parent c4dc74d commit d4432df
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 112 deletions.
2 changes: 1 addition & 1 deletion _config/aws.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
Name: spindb-aws
---
SilverStripe\Core\Injector\Injector:
Injector:
Aws\S3\S3Client.spindb:
factory: LittleGiant\SpinDB\Storage\S3ClientFactory
8 changes: 4 additions & 4 deletions _config/environmentcheck.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
---
Name: spindb-envcheck
---
SilverStripe\EnvironmentCheck\EnvironmentCheckSuite:
EnvironmentCheckSuite:
registered_suites:
health:
- spindb
registered_checks:
spindb:
definition: 'LittleGiant\SpinDB\Health\BackupCheck'
title: 'Is the database backup saving to S3 daily?'
registered_suites:
check:
- spindb
2 changes: 1 addition & 1 deletion _config/spindb.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
Name: spindb
---
SilverStripe\Core\Injector\Injector:
Injector:
Spatie\DbDumper\DbDumper:
factory: LittleGiant\SpinDB\Database\DBDumperFactory
LittleGiant\SpinDB\Database\Dumper:
Expand Down
9 changes: 4 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
"license": "BSD-3-Clause",
"description": "Backup DB periodically and backup to AWS S3 storage with rotation",
"require": {
"silverstripe/framework": "^4.1",
"silverstripe/vendor-plugin": "^1.0",
"silverstripe/crontask": "^2.1",
"silverstripe/framework": "^3.1",
"silverstripe/crontask": "^1.2.0",
"aws/aws-sdk-php": "^3.69",
"silverstripe/environmentcheck": "^2.1",
"spatie/db-dumper": "^2.13"
"silverstripe/environmentcheck": "^1.3.0",
"spatie/db-dumper": "~1.5.1"
},
"authors": [
{
Expand Down
59 changes: 29 additions & 30 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,73 +43,72 @@ composer require littlegiant/silverstripe-spindb
Configure access to your AWS bucket. The below environment variables should be configured either directly
as env on your server, or in `.env` in your project root.

```dotenv
SPINDB_AWS_S3_BUCKET="<thebucketname>"
SPINDB_AWS_REGION="ap-southeast-2" # Or your aws region
SPINDB_AWS_ACCESS_KEY_ID="<my-access-key>"
SPINDB_AWS_SECRET_ACCESS_KEY="<my-secret>"
```php
define('SPINDB_AWS_S3_BUCKET', '<thebucketname>');
define('SPINDB_AWS_REGION', 'ap-southeast-2'); # Or your aws region
define('SPINDB_AWS_ACCESS_KEY_ID', '<my-access-key>');
define('SPINDB_AWS_SECRET_ACCESS_KEY', '<my-secret>');
```

If you want to authenticate via your local AWS credentials (Stored in `~/.aws/credentials`) then
you can provide a profile name instead.

```dotenv
SPINDB_AWS_S3_BUCKET="<thebucketname>"
SPINDB_AWS_REGION="ap-southeast-2" # Or your aws region
SPINDB_AWS_PROFILE="profilename" # The profile name containing your authentication credentials. Can be `default`
```php
define('SPINDB_AWS_S3_BUCKET', '<thebucketname>');
define('SPINDB_AWS_REGION', 'ap-southeast-2'); # Or your aws region
define('SPINDB_AWS_PROFILE', 'profilename'); # The profile name containing your authentication credentials. Can be `default`
```

If you are running this site on AWS you can provide access via IAM instead, and you only need to
specify the following. This is the bare minimum configuration necessary for the module to run.

```dotenv
SPINDB_AWS_S3_BUCKET="<thebucketname>"
SPINDB_AWS_REGION="ap-southeast-2" # Or your aws region
```php
define('SPINDB_AWS_S3_BUCKET', '<thebucketname>');
define('SPINDB_AWS_REGION', 'ap-southeast-2'); # Or your aws region
```

By default DB backups are written to the `{baseurl}/db_{date}{ext}` path within the bucket, but this can be configured.

```dotenv
SPINDB_PATH="{baseurl}/db_{date}{ext}"
```php
define('SPINDB_PATH', '{baseurl}/db_{date}{ext}"
```

Supported vars:
- `{baseurl}` Value of `BASE_URL` var
- `{date}` Date the archive was created (ISO_8601)
- `{time}` Time the archive was created (ISO_8601)
- `{ext}` File extension created, e.g. `.sql` or `.zip` depending on archive method
- `{ext}` File extension created, e.g. `.sql`

## Schedule / rotation configuration

You can configure the time of day that the task occurs, or even how frequently it runs.

```dotenv
SPINDB_SCHEDULE="0 2 * * *" # Every night at 2am
```php
define('SPINDB_SCHEDULE', '0 2 * * *'); # Every night at 2am
```

If you want to backup less frequently you can adjust the day

```dotenv
SPINDB_SCHEDULE="0 2 */2 * *" # Every second night at 2am
```php
define('SPINDB_SCHEDULE', '0 2 */2 * *'); # Every second night at 2am
```

You can configure the number of daily, weekly, monthly, and yearly backups

For each of the below, 0 means keep no backups, -1 means keep unlimited backups (use with care)

```dotenv
SPINDB_KEEP_DAILY="7" # Default to 1 week of backups
SPINDB_KEEP_WEEKLY="0" # Default to no weekly backups
SPINDB_KEEP_WEEKLY_DAY="0" # If keeping weekly backups set the day of the week to keep (0/7 = sunday, 1 = monday, etc).
SPINDB_KEEP_MONTHLY="4" # Default to 4 months of monthly backups
SPINDB_KEEP_MONTHLY_DAY="1" # Day of the month to keep. Archaic 1-based index sorry.
SPINDB_KEEP_YEARLY="-1" # Default to keep unlimited yearly backups.
SPINDB_KEEP_YEARLY_DAY="0" # Day of the year to keep. 0-365. (0 is Jan 1)
SPINDB_ARCHIVE="gzip" # Set archive mode. Supports `gzip` / `none`
```php
define('SPINDB_KEEP_DAILY', '7'); # Default to 1 week of backups
define('SPINDB_KEEP_WEEKLY', '0'); # Default to no weekly backups
define('SPINDB_KEEP_WEEKLY_DAY', '0'); # If keeping weekly backups set the day of the week to keep (0/7 = sunday, 1 = monday, etc).
define('SPINDB_KEEP_MONTHLY', '4'); # Default to 4 months of monthly backups
define('SPINDB_KEEP_MONTHLY_DAY', '1'); # Day of the month to keep. Archaic 1-based index sorry.
define('SPINDB_KEEP_YEARLY', '-1'); # Default to keep unlimited yearly backups.
define('SPINDB_KEEP_YEARLY_DAY', '0'); # Day of the year to keep. 0-365. (0 is Jan 1)
```

You can also configure an alert email to notify when a backup is created.

```dotenv
SPINDB_ALERT_EMAIL="[email protected]"
```php
define('SPINDB_ALERT_EMAIL', '[email protected]');
```
51 changes: 14 additions & 37 deletions src/Configuration/RotateConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@
namespace LittleGiant\SpinDB\Configuration;

use Exception;
use SilverStripe\Control\Director;
use SilverStripe\Core\Convert;
use SilverStripe\Core\Environment;
use SilverStripe\ORM\FieldType\DBDate;
use SilverStripe\ORM\FieldType\DBDatetime;
use Director;
use Convert;
use SS_Datetime;

class RotateConfig
{
const METHOD_GZIP = 'gzip';

const METHOD_NONE = 'none';

/**
Expand All @@ -38,8 +34,8 @@ protected static function getFixedArgs(): array
public static function getCurrentArgs(): array
{
return [
'date' => DBDatetime::now()->Format(DBDate::ISO_DATE),
'time' => DBDatetime::now()->Format('HH.mm.ss'),
'date' => SS_Datetime::now()->Format('y-MM-dd'),
'time' => SS_Datetime::now()->Format('HH.mm.ss'),
];
}

Expand All @@ -63,7 +59,7 @@ protected static function getVariableArgPatterns(): array
*/
public static function schedule(): string
{
return Environment::getEnv('SPINDB_SCHEDULE') ?: '0 2 * * *';
return defined('SPINDB_SCHEDULE') ? SPINDB_SCHEDULE : '0 2 * * *';
}

/**
Expand All @@ -76,7 +72,7 @@ public static function schedule(): string
public static function path($arguments = []): string
{
$arguments = array_merge(self::getFixedArgs(), $arguments);
$pattern = Environment::getEnv('SPINDB_PATH') ?: '{baseurl}/db_{date}{ext}';
$pattern = defined('SPINDB_PATH') ? SPINDB_PATH : '{baseurl}/db_{date}{ext}';
if (!strstr($pattern, '{date}')) {
throw new Exception('SPINDB_PATH variable must contain {date}');
}
Expand Down Expand Up @@ -132,7 +128,7 @@ public static function parse($path): ?array
*/
public static function bucket(): ?string
{
return Environment::getEnv('SPINDB_AWS_S3_BUCKET') ?: null;
return defined('SPINDB_AWS_S3_BUCKET') ? SPINDB_AWS_S3_BUCKET : null;
}

/**
Expand All @@ -142,7 +138,7 @@ public static function bucket(): ?string
*/
public static function region(): ?string
{
return Environment::getEnv('SPINDB_AWS_REGION') ?: null;
return defined('SPINDB_AWS_REGION') ? SPINDB_AWS_REGION : null;
}

/**
Expand All @@ -152,7 +148,7 @@ public static function region(): ?string
*/
public static function accesKeyID(): ?string
{
return Environment::getEnv('SPINDB_AWS_ACCESS_KEY_ID') ?: null;
return defined('SPINDB_AWS_ACCESS_KEY_ID') ? SPINDB_AWS_ACCESS_KEY_ID : null;
}

/**
Expand All @@ -162,7 +158,7 @@ public static function accesKeyID(): ?string
*/
public static function secretAccessKey(): ?string
{
return Environment::getEnv('SPINDB_AWS_SECRET_ACCESS_KEY') ?: null;
return defined('SPINDB_AWS_SECRET_ACCESS_KEY') ? SPINDB_AWS_SECRET_ACCESS_KEY : null;
}

/**
Expand All @@ -172,7 +168,7 @@ public static function secretAccessKey(): ?string
*/
public static function profile(): ?string
{
return Environment::getEnv('SPINDB_AWS_PROFILE') ?: null;
return defined('SPINDB_AWS_PROFILE') ? SPINDB_AWS_PROFILE : null;
}

/**
Expand Down Expand Up @@ -254,29 +250,13 @@ public static function keepYearlyDay(): int
*/
protected static function getNumeric(string $var, int $default): int
{
$daily = Environment::getEnv($var);
$daily = defined($var) ? constant($var) : null;
if (is_numeric($daily)) {
return (int)$daily;
}
return $default;
}

/**
* Get archive method to use
*
* @return string|null
*/
public static function archiveMethod(): ?string
{
$method = Environment::getEnv('SPINDB_ARCHIVE');
switch ($method) {
case self::METHOD_NONE:
return null;
case self::METHOD_GZIP:
default:
return self::METHOD_GZIP;
}
}

/**
* Get file extension to use (not including .)
Expand All @@ -285,9 +265,6 @@ public static function archiveMethod(): ?string
*/
public static function extension(): string
{
$method = static::archiveMethod();
return $method
? "sql.{$method}"
: 'sql';
return 'sql';
}
}
21 changes: 8 additions & 13 deletions src/Database/DBDumperFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@
namespace LittleGiant\SpinDB\Database;

use InvalidArgumentException;
use LittleGiant\SpinDB\Configuration\RotateConfig;
use SilverStripe\Core\Injector\Factory;
use SilverStripe\ORM\DB;
use Spatie\DbDumper\Compressors\GzipCompressor;
use SilverStripe\Framework\Injector\Factory;
use Spatie\DbDumper\Databases\MySql;
use Spatie\DbDumper\Databases\PostgreSql;
use Spatie\DbDumper\Databases\Sqlite;
use Spatie\DbDumper\DbDumper;

/**
Expand All @@ -27,7 +23,10 @@ class DBDumperFactory implements Factory
public function create($service, array $params = array())
{
// Build class from type field
$args = DB::getConfig();

global $databaseConfig;

$args = $databaseConfig;
$backend = $this->getBackend($args['type']);

// Mandatory arguments
Expand All @@ -41,17 +40,12 @@ public function create($service, array $params = array())
$backend->setPort((int)$args['port']);
}

// Set compression (note: Only GZIP supported at the moment)
if (RotateConfig::archiveMethod() === RotateConfig::METHOD_GZIP) {
$backend->useCompressor(new GzipCompressor());
}

return $backend;
}

/**
* @param string $type
* @return DbDumper
* @return DbDumper|MySql|PostgreSql
*/
protected function getBackend($type)
{
Expand All @@ -65,7 +59,8 @@ protected function getBackend($type)
case 'SQLite3Database':
case 'SQLitePDODatabase':
case 'SQLiteDatabase':
return Sqlite::create();
throw new InvalidArgumentException("{$type} is not supported");
break;
case 'PostgrePDODatabase':
case 'PostgreSQLDatabase':
return PostgreSql::create();
Expand Down
9 changes: 5 additions & 4 deletions src/Health/BackupCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace LittleGiant\SpinDB\Health;

use EnvironmentCheck;
use Exception;
use LittleGiant\SpinDB\Storage\RotateStorage;
use SilverStripe\EnvironmentCheck\EnvironmentCheck;
use SilverStripe\ORM\FieldType\DBDatetime;
use SS_Datetime;

class BackupCheck implements EnvironmentCheck
{
Expand Down Expand Up @@ -36,11 +36,12 @@ public function check()

// If best result is < 1 day ago, success
$message = "Last backup {$lastBackupText}";

switch (true) {
case $lastBackup > strtotime("-1 day", DBDatetime::now()->getTimestamp()):
case $lastBackup > strtotime("-1 day", strtotime(SS_Datetime::now()->getValue())):
$status = EnvironmentCheck::OK;
break;
case $lastBackup > strtotime("-2 day", DBDatetime::now()->getTimestamp()):
case $lastBackup > strtotime("-2 day", strtotime(SS_Datetime::now()->getValue())):
$status = EnvironmentCheck::WARNING;
break;
default:
Expand Down
7 changes: 2 additions & 5 deletions src/Storage/RotateStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@
use Aws\S3\S3Client;
use Exception;
use LittleGiant\SpinDB\Configuration\RotateConfig;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Core\Injector\Injector;
use Object;

class RotateStorage
class RotateStorage extends Object
{
use Injectable;

/**
* s3 client
*
Expand Down
1 change: 0 additions & 1 deletion src/Storage/S3ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Aws\Credentials\Credentials;
use Aws\S3\S3Client;
use LittleGiant\SpinDB\Configuration\RotateConfig;
use SilverStripe\Core\Injector\Factory;

class S3ClientFactory implements Factory
{
Expand Down
Loading

0 comments on commit d4432df

Please sign in to comment.