Created
February 9, 2017 17:36
-
-
Save itzikbenh/dc80833035709d62dcfa21707efd80eb to your computer and use it in GitHub Desktop.
Laravel backup command to amazon S3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Console\Commands; | |
| use Aws\S3\MultipartUploader; | |
| use Aws\Exception\MultipartUploadException; | |
| use Illuminate\Console\Command; | |
| use Storage; | |
| class BackupCRM extends Command | |
| { | |
| /** | |
| * The name and signature of the console command. | |
| * | |
| * @var string | |
| */ | |
| protected $signature = 'db:backup {--only-db} {--only-uploads}'; | |
| /** | |
| * The console command description. | |
| * | |
| * @var string | |
| */ | |
| protected $description = 'Backups datatbase and uploads.'; | |
| /** | |
| * Create a new command instance. | |
| * | |
| * @return void | |
| */ | |
| public function __construct() | |
| { | |
| parent::__construct(); | |
| } | |
| /** | |
| * Execute the console command. | |
| * | |
| * @return mixed | |
| */ | |
| public function handle() | |
| { | |
| if ($this->option('only-db')) { | |
| return $this->backupDB(env('DB_USERNAME'), env('DB_PASSWORD'), env('DB_HOST'), env('DB_DATABASE')); | |
| } else if ($this->option('only-uploads')) { | |
| return $this->backupUploads(env('S3_BUCKET')); | |
| } else { | |
| $this->backupDB(env('DB_USERNAME'), env('DB_PASSWORD'), env('DB_HOST'), env('DB_DATABASE')); | |
| $this->backupUploads(env('S3_BUCKET')); | |
| return $this->info('Full backup completed!'); | |
| } | |
| } | |
| protected function backupDB($user, $password, $host, $db) | |
| { | |
| $filePrefix = date('m-d-y-h:i:s'); | |
| $fileSql = $filePrefix.'file.sql'; | |
| exec('mysqldump --result-file=storage/app/'.$fileSql.' --user='.$user.' --password='.$password.' --host='.$host.' '.$db); | |
| exec('gzip storage/app/'.$fileSql); | |
| Storage::disk('s3')->put('database-backups/'. date("Y") . '/' . date("m") . '/'.$filePrefix.'file.sql.gz' , fopen('storage/app/'.$filePrefix.'file.sql.gz', 'r+')); | |
| Storage::delete($filePrefix.'file.sql.gz'); | |
| return $this->info('Database backup completed!'); | |
| } | |
| protected function backupUploads($bucket) | |
| { | |
| $uploads = date('m-d-y-h:i:s').'uploads.tar.gz'; | |
| exec('tar -zcf storage/app/'.$uploads.' public/uploads/'); | |
| $disk = Storage::disk('s3'); | |
| $fromPath = 'storage/app/'.$uploads; | |
| $toPath = 'uploads-backups/'. date("Y") . '/' . date("m") . '/'.$uploads; | |
| $uploader = new MultipartUploader($disk->getDriver()->getAdapter()->getClient(), $fromPath, [ | |
| 'bucket' => $bucket, | |
| 'key' => $toPath, | |
| ]); | |
| try { | |
| $result = $uploader->upload(); | |
| $this->info('Uploads backup completed!'); | |
| Storage::delete($uploads); | |
| } catch (MultipartUploadException $e) { | |
| echo $e->getMessage(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment