-
-
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.
- Loading branch information
Showing
8 changed files
with
450 additions
and
67 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
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); | ||
} | ||
} |
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,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" | ||
} | ||
} | ||
} | ||
] | ||
} |
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 |
---|---|---|
@@ -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" | ||
} | ||
} |
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.