S3Backup Laravel Command

Introduction

This package contains three command line commands for Laravel to upload files to Amazon S3. This will allow you to schedule backups either as cron jobs or via Laravel's scheduler or to backup files from the command line.


Installation

composer require escuccim/s3backup

Then register the components in /config/app.php:

Add the following to the 'providers' array:

Escuccim\S3Backup\S3BackupServiceProvider::class,
Aws\Laravel\AwsServiceProvider::class,

You must add the following values to your .env file:

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_REGION=
AWS_BUCKET=

These values are used to login to your AWS account and upload the files.


Usage

Currently three separate commands are created: to upload a single file, to upload an entire directory, and to dump a MySQL database and upload the dump file. Note that you should not use leading or trailing "/"s on any of the paths.

To Dump And Upload Database

The followwing command will execute a MySQL dump and then upload the file to S3.

php artisan backup:db {--path=} {--db=} {--user=} {--dest=} {--keep} {--p} {--pass=}

The options are as follows:

  • --path= (optional) - by default the dump will be written in the /database/ directory. If you wish to use a different directory specify that here, relative to the /database directory.
  • --db= (optional) - use this to specify a database to dump, if not specified the DB will be taken from the .env file
  • --user= (optional) - to specify a username to use for the dump, if none is specified the username will be taken from the .env file
  • --dest= (optional) - the path to upload the dump to at S3. By default it will be uploaded to the root of the bucket specified in the .env file.
  • --keep (optional) - by default the dump will be deleted after being uploaded. If you wish to keep the local copy of the dump file add this flag with no value.
  • --p (optional) - specifies whether a password should be use. If you either do not have a password for the user specified or have credentials stored in a .my.cnf file you can omit this.
  • --pass= (optional) - specifies a password to be used for the dump. If you use this parameter you must also use --p. If --p is included but no password is passed the password will be taken from the .env file

If you do not pass any parameters everything will be taken from the .env file.

To Upload a Single File

The following command will upload a single file to S3:

php artisan backup:file [file] [--dest=location to upload file to]

The parameters are as follows:

  • file (required) - the path to the file to upload, relative to your project root. After being upload the file will keep the same name.
  • --dest= (optional) - the path to upload the file to, relative to the root of your bucket.

To Upload Directories

The following command will recursively upload a directory and its subdirectories to S3:

php artisan backup:dir [path] [--dest=location to upload file to] [--ext=file extension]

The parameters are as follows:

  • path (required) - the path to the directory to upload relative to your project root. The directory and its subdirectories will retain the same names after upload.
  • --dest= (optional) - the path to upload the files to, relative to the root of your bucket.
  • --ext= (optional) - will only upload files matching this extension, if one is specified.

Configuration

As mentioned above, you must add the following values to your .env file:

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_REGION=
AWS_BUCKET=

There is no other configuration for this package.