PHP code example of oluokunkabiru / database-exporter
1. Go to this page and download the library: Download oluokunkabiru/database-exporter library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
oluokunkabiru / database-exporter example snippets
use Oluokunkabiru\DatabaseExporter\DatabaseExporter;
use Oluokunkabiru\DatabaseExporter\ConnectionResolver;
// Automatically detect connection
$exporter = new DatabaseExporter(ConnectionResolver::resolve());
// Export entire database
$filepath = $exporter
->compress(true)
->addDropTable(true)
->export();
echo "Backup saved to: " . $filepath;
// Export specific tables
$filepath = $exporter
->tables(['users', 'posts', 'comments'])
->format('csv')
->export();
// Get database information
$info = $exporter->getDatabaseInfo();
echo "Database: " . $info['name'];
echo "Tables: " . $info['table_count'];
echo "Size: " . $info['size_formatted'];
use Oluokunkabiru\DatabaseExporter\DatabaseExporter;
use PDO;
$connection = new PDO(
'mysql:host=localhost;dbname=my_database;charset=utf8mb4',
'root',
'password',
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]
);
$exporter = new DatabaseExporter($connection);
$filepath = $exporter
->tables(['users', 'posts'])
->format('sql')
->compress(true)
->export();
echo "Export completed: " . basename($filepath);
use Oluokunkabiru\DatabaseExporter\ConnectionResolver;
use Oluokunkabiru\DatabaseExporter\DatabaseExporter;
// Set environment variables
$_ENV['DB_HOST'] = 'localhost';
$_ENV['DB_PORT'] = 3306;
$_ENV['DB_DATABASE'] = 'my_database';
$_ENV['DB_USERNAME'] = 'root';
$_ENV['DB_PASSWORD'] = '';
// Automatically resolve connection
$connection = ConnectionResolver::resolveFromEnv();
$exporter = new DatabaseExporter($connection);
$filepath = $exporter->export();
return [
// Enable/disable the web dashboard
'dashboard_enabled' => true,
// Route prefix for dashboard
'route_prefix' => 'database-exporter',
// Default export format (sql or csv)
'default_format' => 'sql',
// Enable compression by default
'enable_compression' => true,
// Directory where exports are stored
'backup_directory' => storage_path('exports'),
// Include database views
'include_views' => false,
// Include database triggers
'include_triggers' => false,
// Include stored procedures
'include_procedures' => false,
// Add DROP TABLE statements
'add_drop_table' => true,
// SQL formatting (pretty or minified)
'sql_format' => 'pretty',
// Max execution time in seconds
'max_execution_time' => 900,
// Require authentication
'
// In app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
$exporter = app('database-exporter');
$filepath = $exporter
->compress(true)
->export();
// Store in cloud
Storage::disk('s3')->put(
'backups/database-' . date('Y-m-d-His') . '.zip',
file_get_contents($filepath)
);
// Clean old backups
$this->deleteOldBackups();
})->daily()->at('02:00');
}
protected function deleteOldBackups()
{
$backupDir = storage_path('exports');
$thirtyDaysAgo = time() - (30 * 24 * 60 * 60);
foreach (glob($backupDir . '/*') as $file) {
if (filemtime($file) < $thirtyDaysAgo) {
unlink($file);
}
}
}