Skip to content

Commit

Permalink
Added backup tool, fixed #14
Browse files Browse the repository at this point in the history
  • Loading branch information
getpinga committed Oct 23, 2024
1 parent 61fc97a commit 490f28d
Show file tree
Hide file tree
Showing 8 changed files with 450 additions and 67 deletions.
136 changes: 71 additions & 65 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,73 +54,73 @@ systemctl restart php8.2-fpm

```bash
server {
listen 80;
server_name %%DOMAIN%%;
return 301 https://%%DOMAIN%%/request_uri/;
listen 80;
server_name %%DOMAIN%%;
return 301 https://%%DOMAIN%%/request_uri/;
}

server {
listen 443 ssl;
http2 on;
ssl_certificate /etc/letsencrypt/live/%%DOMAIN%%/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/%%DOMAIN%%/privkey.pem;
ssl_stapling on;
ssl_stapling_verify on;

set $root_path '%%SOURCE_PATH%%';
server_name %%DOMAIN%%;

index index.php;
root $root_path;
try_files $uri $uri/ @rewrite;
sendfile off;
include /etc/nginx/mime.types;

# Block access to sensitive files and return 404 to make it indistinguishable from a missing file
location ~* .(ini|sh|inc|bak|twig|sql)$ {
return 404;
}

# Block access to hidden files except .well-known
location ~ /\.(?!well-known\/) {
return 404;
}

# Disable PHP execution in /uploads
location ~* /uploads/.*\.php$ {
return 404;
}

# Deny access to /data
location ~* /data/ {
return 404;
}

location @rewrite {
rewrite ^/page/(.*)$ /index.php?_url=/custompages/$1;
rewrite ^/(.*)$ /index.php?_url=/$1;
}

location ~ \.php {
fastcgi_split_path_info ^(.+\.php)(/.+)$;

# fastcgi_pass need to be changed according your server setup:
# phpx.x is your server setup
# examples: /var/run/phpx.x-fpm.sock, /var/run/php/phpx.x-fpm.sock or /run/php/phpx.x-fpm.sock are all valid options
# Or even localhost:port (Default 9000 will work fine)
# Please check your server setup

fastcgi_pass unix:/run/php/php8.2-fpm.sock;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
include fastcgi_params;
}

location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
root $root_path;
expires off;
}
listen 443 ssl;
http2 on;
ssl_certificate /etc/letsencrypt/live/%%DOMAIN%%/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/%%DOMAIN%%/privkey.pem;
ssl_stapling on;
ssl_stapling_verify on;

set $root_path '%%SOURCE_PATH%%';
server_name %%DOMAIN%%;

index index.php;
root $root_path;
try_files $uri $uri/ @rewrite;
sendfile off;
include /etc/nginx/mime.types;

# Block access to sensitive files and return 404 to make it indistinguishable from a missing file
location ~* .(ini|sh|inc|bak|twig|sql)$ {
return 404;
}

# Block access to hidden files except .well-known
location ~ /\.(?!well-known\/) {
return 404;
}

# Disable PHP execution in /uploads
location ~* /uploads/.*\.php$ {
return 404;
}

# Deny access to /data
location ~* /data/ {
return 404;
}

location @rewrite {
rewrite ^/page/(.*)$ /index.php?_url=/custompages/$1;
rewrite ^/(.*)$ /index.php?_url=/$1;
}

location ~ \.php {
fastcgi_split_path_info ^(.+\.php)(/.+)$;

# fastcgi_pass need to be changed according your server setup:
# phpx.x is your server setup
# examples: /var/run/phpx.x-fpm.sock, /var/run/php/phpx.x-fpm.sock or /run/php/phpx.x-fpm.sock are all valid options
# Or even localhost:port (Default 9000 will work fine)
# Please check your server setup

fastcgi_pass unix:/run/php/php8.2-fpm.sock;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
include fastcgi_params;
}

location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
root $root_path;
expires off;
}
}
```

Expand Down Expand Up @@ -417,4 +417,10 @@ Navigate to https://github.com/getnamingo/fossbilling-dns and follow the install

2. In your contact page, you will need to list all company details, including registration number and name of CEO.

3. Some manual tune-in is still required in various parts.
3. Some manual tune-in is still required in various parts.

### Setup Backup

To ensure the safety and availability of your data in Namingo, it's crucial to set up and verify automated backups. Begin by editing the backup.json file in the automation directory, where you'll input your database details. Ensure that the details for the database are accurately entered in two specified locations within the backup.json file.

Additionally, check that the cronjob for PHPBU is correctly scheduled on your server `cron.php`, as this automates the backup process. You can verify this by reviewing your server's cronjob list. These steps are vital to maintain regular, secure backups of your system, safeguarding against data loss and ensuring business continuity.
110 changes: 110 additions & 0 deletions automation/backup-upload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

require __DIR__ . '/vendor/autoload.php';

require_once 'helpers.php';

use League\Flysystem\Filesystem;
use League\Flysystem\PhpseclibV3\SftpConnectionProvider;
use League\Flysystem\PhpseclibV3\SftpAdapter;
use League\Flysystem\UnixVisibility\PortableVisibilityConverter;
use Spatie\FlysystemDropbox\DropboxAdapter;
use Hypweb\Flysystem\GoogleDrive\GoogleDriveAdapter;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use League\Flysystem\AdapterInterface;

$logFilePath = '/var/log/backup_upload.log';
$log = setupLogger($logFilePath, 'Backup_Upload');
$log->info('job started.');

// Storage type: 'sftp', 'dropbox', or 'google_drive'
$storageType = 'sftp'; // Set this to your preferred storage

// Setup the filesystem based on the storage type
switch ($storageType) {
case 'sftp':
$sftpProvider = new SftpConnectionProvider(
'your_sftp_host', // host
'your_username', // username
'your_password', // password
'/path/to/my/private_key', // private key
'passphrase', // passphrase
22, // port
true, // use agent
30, // timeout
10, // max tries
'fingerprint-string' // host fingerprint
// connectivity checker (optional)
);

$visibilityConverter = PortableVisibilityConverter::fromArray([
'file' => [
'public' => 0640,
'private' => 0604,
],
'dir' => [
'public' => 0740,
'private' => 7604,
],
]);

$adapter = new SftpAdapter($sftpProvider, '/upload', $visibilityConverter);
break;
case 'dropbox':
$client = new \Spatie\Dropbox\Client('your_dropbox_access_token');
$adapter = new DropboxAdapter($client);
break;
case 'google_drive':
$client = new \Google\Client();
$client->setClientId('your_client_id');
$client->setClientSecret('your_client_secret');
$client->refreshToken('your_refresh_token');
$service = new \Google\Service\Drive($client);
$adapter = new GoogleDriveAdapter($service, 'your_folder_id');
break;
default:
$log->error("Invalid storage type");
exit;
}

$filesystem = new Filesystem($adapter);

// Function to upload a file with try-catch for error handling
function uploadFile($filesystem, $localPath, $remotePath, $logger) {
try {
if (file_exists($localPath)) {
$stream = fopen($localPath, 'r+');
$filesystem->writeStream($remotePath, $stream);
if (is_resource($stream)) {
fclose($stream);
}
$logger->info("Uploaded: $localPath to $remotePath");
} else {
$logger->warning("File not found: $localPath");
}
} catch (Exception $e) {
$logger->error("Error uploading $localPath: " . $e->getMessage());
}
}

// Current date and hour in the specified format
$currentDateHour = date('Ymd-H'); // Format: YYYYMMDD-HH

// Directory to check
$directory = '/srv/';

// Pattern to match files
$pattern = "/^database-$currentDateHour.*\.sql\.bz2$/";
$pattern2 = "/^files-$currentDateHour.*\.bz2$/";
$pattern3 = "/^registrarweb-$currentDateHour.*\.bz2$/";

// Scan directory for matching files
$files = scandir($directory);
foreach ($files as $file) {
if (preg_match($pattern, $file) || preg_match($pattern2, $file) || preg_match($pattern3, $file)) {
$localPath = $directory . $file;
$remoteFileName = basename($file);
uploadFile($filesystem, $localPath, $remoteFileName, $log);
}
}
93 changes: 93 additions & 0 deletions automation/backup.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
{
"verbose": true,
"logging": [
{
"type": "json",
"target": "/var/log/backup.log"
}
],
"backups": [
{
"name": "Database",
"source": {
"type": "mysqldump",
"options": {
"databases": "registrar",
"user": "your_username",
"password": "your_password"
}
},
"target": {
"dirname": "/srv",
"filename": "database-%Y%m%d-%H%i.sql",
"compress": "bzip2"
},
"checks": [
{
"type": "sizemin",
"value": "10M"
}
],
"cleanup": {
"type": "Capacity",
"options": {
"size": "750M"
}
}
},
{
"name": "Files",
"source": {
"type": "tar",
"options": {
"path": "/opt/registrar",
"throttle": "5m"
}
},
"target": {
"dirname": "/srv",
"filename": "files-%Y%m%d-%H%i",
"compress": "bzip2"
},
"checks": [
{
"type": "sizemin",
"value": "10M"
}
],
"cleanup": {
"type": "Capacity",
"options": {
"size": "750M"
}
}
},
{
"name": "Files",
"source": {
"type": "tar",
"options": {
"path": "/var/www",
"throttle": "5m"
}
},
"target": {
"dirname": "/srv",
"filename": "registrarweb-%Y%m%d-%H%i",
"compress": "bzip2"
},
"checks": [
{
"type": "sizemin",
"value": "10M"
}
],
"cleanup": {
"type": "Capacity",
"options": {
"size": "750M"
}
}
}
]
}
7 changes: 6 additions & 1 deletion automation/composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
{
"require": {
"peppeocchi/php-cron-scheduler": "^4.0",
"phpmailer/phpmailer": "^6.9.1"
"phpmailer/phpmailer": "^6.9.1",
"phpseclib/phpseclib": "^3.0",
"phpbu/phpbu": "^6.0",
"monolog/monolog": "^3.7",
"league/flysystem": "^3.28",
"league/flysystem-sftp-v3": "^3.28"
}
}
10 changes: 10 additions & 0 deletions automation/cron.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?php

// Configuration
$cronJobConfig = [
'backup' => false, // Set to true to enable
];

require __DIR__ . '/vendor/autoload.php';

use GO\Scheduler;
Expand All @@ -13,4 +18,9 @@
$scheduler->php('/opt/registrar/automation/errp_dns.php')->at('0 2 * * *');
$scheduler->php('/opt/registrar/automation/urs.php')->at('45 * * * *');

if ($cronJobConfig['backup']) {
$scheduler->raw('/opt/registrar/automation/vendor/bin/phpbu --configuration=/opt/registrar/automation/backup.json')->at('15 * * * *');
$scheduler->php('/opt/registrar/automation/backup-upload.php')->at('30 * * * *');
}

$scheduler->run();
Loading

0 comments on commit 490f28d

Please sign in to comment.