dutchie027 / easymysqlbackup
这个库依赖于mysqldump的功能,可以创建Maria或MySQL数据库的备份。它允许您本地保存或上传到云存储。
0.4.0
2022-04-04 01:01 UTC
Requires
- php: >=7.4
- aws/aws-sdk-php: ^3.216
- monolog/monolog: ^2.4
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.8
- mockery/mockery: ^1.5
- phpstan/phpstan: ^1.5
- phpunit/phpunit: ^9
- psalm/phar: ^4.22
This package is auto-updated.
Last update: 2024-09-22 15:41:28 UTC
README
关于
此包允许您使用mysqldump备份mysql数据库,并将其存储到本地文件存储(默认)或上传到S3。
安装
使用Composer将此包作为依赖项安装。
composer require dutchie027/easymysqlbackup
您还需要确保在托管此代码的机器上有一个mysqldump的副本。如果您想使用压缩备份和恢复,则需要gzip和gunzip。
用法
如果没有配置文件,程序将假定许多默认值,但是强烈建议您创建一个配置(请参阅下面的示例.ini文件)
本地备份
#!/usr/bin/php <?php include_once 'vendor/autoload.php'; use dutchie027\EasyMySQLBackup\Backup; # OPTION A: Create a new backup with "default" configuration set $backup = new Backup(); # OPTION B: Create a configuration set using an .ini file $backup = new Backup('/path/to/my.ini'); # Backup the database named "test". The location on the file system will be returned $backup_file = $backup->createLocalBackup("test");
备份并上传到S3
#!/usr/bin/php <?php include_once 'vendor/autoload.php'; use dutchie027\EasyMySQLBackup\Backup; # Because we're using S3, we have to instatiate it with a configuration set using an .ini file $backup = new Backup('/path/to/my.ini'); # Backup the database named "test". The location on the file system will be returned $backup_file = $backup->createLocalBackup("test"); # Upload the $backup_name file to the S3 Location "my-sql-backups/database.12345.sql.gz" # NOTE: It will chop and assume the bucket name of "my-sql-backups". If the bucket "my-sql-backups" doesn't exist, it will create it # NOTE 2: You can use multiple depts to the S3 key name # NOTE 3: If you end the second parameter in anything other than .sql or .gz it will assume # you are using a directory and append the name of the backup file to the end of the structure $backup->s3->uploadFile($backup_file, "my-sql-backups/database.12345.sql.gz"); # Using the initial connection, remove the local file $backup->purgeBackup();
恢复本地文件
#!/usr/bin/php <?php include_once 'vendor/autoload.php'; use dutchie027\EasyMySQLBackup\Backup; # OPTION A: Create a new backup with "default" configuration set $backup = new Backup(); # Restore the file '/backups/mydb.20220330162457.sql.gz' to the database named 'restoredb' # Also, force the database to be dropped & recreated $backup->restore()->restoreLocalBackup('/backups/mydb.20220330162457.sql.gz', 'restoredb', 1);
从S3恢复
#!/usr/bin/php <?php include_once 'vendor/autoload.php'; use dutchie027\EasyMySQLBackup\Backup; # Because we're using S3, we have to give it a config file with our KVPs for S3 Access in them $backup = new Backup('/path/to/my.ini'); # First download the file coredb.20220330162457.sql.gz from the bob-test bucket and put it in /tmp # Assuming all runs well, store the local file name in the variable $buf $buf = $backup->s3()->downloadFile('bob-test/coredb.20220330162457.sql.gz', '/tmp'); # Restore the newly downloaded $buf file to a new database 'core-restore' and force it to be dropped # and created fresh $backup->restore()->restoreLocalBackup($buf, 'core-restore', 1);
示例my.ini(显示所有值)
[s3] S3_REGION = 'us-east-1' S3_ENDPOINT = "https://s3.us-east-1.amazonaws.com" S3_ACCESS_KEY = "ABCD1234EFGH5678ZZZZ" S3_SECRET_KEY = "JuStiN8675309NeEDedA30918KeYtoTest567890" S3_ACL = "private" [database] DB_USER = 'root' DB_PASSWORD = '' [log] LOG_LEVEL = 300 ;Matches Constants found https://github.com/Seldaek/monolog/blob/main/src/Monolog/Logger.php LOG_PREFIX = 'easyMySQLBackup' LOG_DIRECTORY = '/var/log/'
待办事项
- 添加程序以允许删除超过"x"天的旧本地备份
- 可能添加其他云存储API
- 清理文档
- 其他事项
行为准则
此项目遵守行为准则。通过参与此项目和其社区,您应遵守此准则。
许可证
Easy MySQL Backup在MIT许可证下发布。有关详细信息,请参阅LICENSE。
版本控制
此代码使用Semver。这意味着版本标记为MAJOR.MINOR.PATCH。只有新的主要版本才允许打破向后兼容性(BC)。
标记为@experimental或@internal的类不包括在我们的向后兼容性承诺中。您也不能保证方法返回的值始终相同。我们保证数据类型不会更改。
贡献
欢迎贡献!要贡献,请熟悉CONTRIBUTING.md。