sebastiansulinski / php-backup
Simple package for backing up databases, files and directories to Dropbox and Ftp.
Installs: 3 654
Dependents: 0
Suggesters: 0
Security: 0
Stars: 14
Watchers: 5
Forks: 6
Open Issues: 0
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
A simple package for backing up mysql databases, files and directories to Dropbox and FTP.
This package makes use of
Usage examples
You can watch this video tutorial or read below.
Backing up to Dropbox and sending Slack notifications along the way
*To send slack
notifications, composer require maknz/slack
and create an incoming 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()); }
Backing up to 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 ); }