diff --git a/automation/backup-upload.php b/automation/backup-upload.php new file mode 100644 index 0000000..433a728 --- /dev/null +++ b/automation/backup-upload.php @@ -0,0 +1,109 @@ +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.*\.sql\.bz2$/"; + +// Scan directory for matching files +$files = scandir($directory); +foreach ($files as $file) { + if (preg_match($pattern, $file) || preg_match($pattern2, $file)) { + $localPath = $directory . $file; + $remoteFileName = basename($file); + uploadFile($filesystem, $localPath, $remoteFileName, $log); + } +} diff --git a/automation/composer.json b/automation/composer.json index cb434f3..6dfdd6b 100644 --- a/automation/composer.json +++ b/automation/composer.json @@ -4,6 +4,8 @@ "phpseclib/phpseclib": "^3.0", "phpbu/phpbu": "^6.0", "setbased/php-audit": "^1.9", - "monolog/monolog": "^3.5" + "monolog/monolog": "^3.5", + "league/flysystem": "^3.23", + "league/flysystem-sftp-v3": "^3.22" } } diff --git a/automation/crontab.example b/automation/crontab.example index e6d6e49..685128e 100644 --- a/automation/crontab.example +++ b/automation/crontab.example @@ -6,8 +6,11 @@ # run statistics.php at 59 min, every hour 59 * * * * root /usr/bin/php8.2 /opt/registry/automation/statistics.php -# run backup at 59 min, every hour -59 * * * * /opt/registry/automation/vendor/bin/phpbu --configuration=/opt/registry/automation/backup.json +# run backup at 15 min, every hour +15 * * * * /opt/registry/automation/vendor/bin/phpbu --configuration=/opt/registry/automation/backup.json + +# run backup-upload.php at 30 min, every hour +30 * * * * root /usr/bin/php8.2 /opt/registry/automation/backup-upload.php # run change-domain-status.php every hour 30 * * * * root /usr/bin/php8.2 /opt/registry/automation/change-domain-status.php