sebastiansulinski / php-backup
一个简单的用于将数据库、文件和目录备份到Dropbox和FTP的软件包。
v3.0.0
2022-06-22 14:54 UTC
Requires
- php: ^8.0
- backup-manager/backup-manager: ^3.0
- illuminate/filesystem: ^9.0
- league/flysystem: ^1.0
- nesbot/carbon: ^2.0
- spatie/flysystem-dropbox: ^1.0
Requires (Dev)
- ext-zip: *
- phpunit/phpunit: ^9.0
README
一个简单的用于将MySQL数据库、文件和目录备份到Dropbox和FTP的软件包。
此软件包使用了以下库
使用示例
您可以观看这个 视频教程 或阅读以下内容。
备份到Dropbox并发送Slack通知
*要发送 slack
通知,请运行 composer require maknz/slack
并 创建一个入站webhook
require "../vendor/autoload.php"; use SSD\DotEnv\DotEnv; use SSD\Backup\Backup; use SSD\Backup\Jobs\File; use SSD\Backup\Jobs\Directory; use SSD\Backup\Remotes\Dropbox; use SSD\Backup\Jobs\MySQLDatabase; use Carbon\Carbon; use Illuminate\Filesystem\Filesystem; use Maknz\Slack\Client as SlackClient; $dotenv = new DotEnv([__DIR__ . '/.env']); $dotenv->load(); $dotenv->required([ 'DROPBOX_OAUTH', 'REMOTE_DIR_NAME', 'DB_HOST', 'DB_PORT', 'DB_NAME', 'DB_USER', 'DB_PASS' ]); // working directory $workingDirectory = __DIR__ . '/tmp'; // Slack client $client = new SlackClient('https://hooks.slack.com/your_slack_webhook', [ 'username' => 'your_slack_username', 'channel' => '#your_slack_channel', 'link_names' => true ]); $client->send('Project backup started at: ' . Carbon::now()->toDateTimeString()); try { $remote = new Dropbox( getenv('DROPBOX_OAUTH') ); $backup = new Backup( $remote, $workingDirectory ); // directory to which backup should be saved on the remote server $backup->setRemoteDirectory(getenv('REMOTE_DIR_NAME')); // keep only 7 backups then overwrite the oldest one $backup->setNumberOfBackups(7); // add MySQL database to the backup $backup->addJob(new Job( new MySQLDatabase([ 'host' => getenv('DB_HOST'), 'name' => getenv('DB_NAME'), 'user' => getenv('DB_USER'), 'password' => getenv('DB_PASS') ]), 'database' )); // add single file to the backup $backup->addJob(new Job( new File( __DIR__ . '/files/text.txt', __DIR__ ), 'files' )); // add the 'files' directory to the backup // but exclude the 'css' directory within $backup->addJob(new Job( new Directory( __DIR__ . '/files', __DIR__, [ 'files/css' ] ), 'files' )); // run backup $backup->run(); } catch (Exception $exception) { $client->send('Project backup failed at: ' . Carbon::now()->toDateTimeString() .' with message: "'.$exception->getMessage().'"'); $filesystem = new Filesystem; $filesystem->cleanDirectory($workingDirectory); $filesystem->prepend( $workingDirectory . DIRECTORY_SEPARATOR . 'error_log', $exception->getMessage() . PHP_EOL ); } finally { $client->send('Project backup finished at: ' . Carbon::now()->toDateTimeString()); }
备份到FTP
require "../vendor/autoload.php"; use SSD\DotEnv\DotEnv; use SSD\Backup\Backup; use SSD\Backup\Jobs\File; use SSD\Backup\Remotes\Ftp; use SSD\Backup\Jobs\Directory; use SSD\Backup\Jobs\MySQLDatabase; use Illuminate\Filesystem\Filesystem; try { $dotenv = new DotEnv([ __DIR__ . '/.env' ]); $dotenv->load(); $dotenv->required([ 'FTP_HOST', 'FTP_USER', 'FTP_PASS', 'REMOTE_DIR_NAME', 'DB_HOST', 'DB_PORT', 'DB_NAME', 'DB_USER', 'DB_PASS' ]); // working directory $workingDirectory = __DIR__ . '/tmp'; $remote = new Ftp( getenv('FTP_HOST'), getenv('FTP_USER'), getenv('FTP_PASS') ); $backup = new Backup( $remote, $workingDirectory ); // directory to which backup should be saved on the remote server $backup->setRemoteDirectory(getenv('REMOTE_DIR_NAME')); // keep only 7 backups then overwrite the oldest one $backup->setNumberOfBackups(7); // add MySQL database to the backup $backup->addJob(new Job( new MySQLDatabase([ 'host' => getenv('DB_HOST'), 'name' => getenv('DB_NAME'), 'user' => getenv('DB_USER'), 'password' => getenv('DB_PASS') ]), 'database' )); // add single file to the backup $backup->addJob(new Job( new File( __DIR__ . '/files/text.txt', __DIR__ ), 'files' )); // add the entire directory to the backup $backup->addJob(new Job( new Directory( __DIR__ . '/files/css', __DIR__ . '/files' ), 'files' )); // run backup $backup->run(); } catch (Exception $e) { $filesystem = new Filesystem; $filesystem->cleanDirectory($workingDirectory); $filesystem->prepend( $workingDirectory . DIRECTORY_SEPARATOR . 'error_log', $e->getMessage() . PHP_EOL ); }