Skip to content

A bash script for pull/push content between environments

License

Notifications You must be signed in to change notification settings

beliven-it/laravel-sync

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

33 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Laravel Sync


GitHub Release


A bash script used to sync the Laravel project contents between different environments.

πŸ’« Usecase

The main scope of the script is used to sync the database and assets between different environments.

For example, you can have a Statamic project in production costantly updated with new content by the client. You need to sync the contents of your local environment to test the new features with a real snapshot of the production environment.

With this script you can now sync the database and assets between the environments 🌟🌟🌟!!!

Warning

⚠️ ⚠️ ⚠️

This script provide a push command to a remote environment. Make sure to have a backup of the remote environment before using the push command.

⚠️ ⚠️ ⚠️



πŸ”— Requirements

The script requires the following tools:

  • 🐳 Docker - Used if you use sail layer.
  • 🌐 SSH - Used to connect to the remote server.



βš™οΈ Installation

You can install the script in your laravel project using the following command:

curl https://raw.githubusercontent.com/beliven-it/laravel-sync/main/install.sh | bash

This command download the latest release of the script and make it executable.

Global installation

You can also move the file to a directory that is in your $PATH, such as /usr/local/bin.

sudo mv laravel-sync /usr/local/bin

This allow you to run the script from any directory.

laravel-sync -h

Note

Make sure to use a tagged version of the script to avoid any issue.



πŸ›  Configuration

The script uses the .env file as configuration.

Make sure to add the following variable to the .env file:

echo "SYNC_ENVIRONMENT_ALLOWED='staging production'" >> .env

If you have a different environment, you can add it to the SYNC_ENVIRONMENT_ALLOWED variable.

# For example if you have staging, uat and production environments
echo "SYNC_ENVIRONMENT_ALLOWED='staging uat production'" >> .env

At the first run:

./laravel-sync config

This command create inside the .env the env needed to use the script. An example of the .env file is:

SYNC_ENVIRONMENT_ALLOWED="staging production"

# Folders to download / upload
SYNC_ENVIRONMENT_ASSETS="storage/statamic/glide/containers content"

# Local environment
# Database environment
SYNC_ENVIRONMENT_LOCAL_DB_HOST="${DB_HOST}"
SYNC_ENVIRONMENT_LOCAL_DB_PORT="${DB_PORT}"
SYNC_ENVIRONMENT_LOCAL_DB_USERNAME="${DB_USERNAME}"
SYNC_ENVIRONMENT_LOCAL_DB_PASSWORD="${DB_PASSWORD}"
SYNC_ENVIRONMENT_LOCAL_DB_DATABASE="${DB_DATABASE}"
SYNC_ENVIRONMENT_LOCAL_DB_CONNECTION="${DB_CONNECTION}"

# Host environment for local is not needed

# Staging environment
# # Database environment
SYNC_ENVIRONMENT_STAGING_DB_HOSTNAME=
SYNC_ENVIRONMENT_STAGING_DB_PORT=
SYNC_ENVIRONMENT_STAGING_DB_PASSWORD=
SYNC_ENVIRONMENT_STAGING_DB_USERNAME=
SYNC_ENVIRONMENT_STAGING_DB_DATABASE=
SYNC_ENVIRONMENT_STAGING_DB_CONNECTION=

# Host environment
SYNC_ENVIRONMENT_STAGING_HOST_USER=
SYNC_ENVIRONMENT_STAGING_HOST_HOSTNAME=
SYNC_ENVIRONMENT_STAGING_HOST_PORT=
SYNC_ENVIRONMENT_STAGING_HOST_BASE_PATH=

# Production environment
# Database environment
SYNC_ENVIRONMENT_PRODUCTION_DB_HOST=
SYNC_ENVIRONMENT_PRODUCTION_DB_PORT=
SYNC_ENVIRONMENT_PRODUCTION_DB_USERNAME=
SYNC_ENVIRONMENT_PRODUCTION_DB_PASSWORD=
SYNC_ENVIRONMENT_PRODUCTION_DB_DATABASE=
SYNC_ENVIRONMENT_PRODUCTION_DB_CONNECTION=

# Host environment
SYNC_ENVIRONMENT_PRODUCTION_HOST_HOSTNAME=
SYNC_ENVIRONMENT_PRODUCTION_HOST_PORT=
SYNC_ENVIRONMENT_PRODUCTION_HOST_USER=
SYNC_ENVIRONMENT_PRODUCTION_HOST_BASE_PATH=



πŸ’» Usage

The script require one of following actions:

  • pull - Pull the database and assets from the remote environment.
  • push - Push the database and assets to the remote environment.
  • config - Setup the .env file.

When you use the pull or push actions, you need to specify the environment. For example:

./laravel-sync pull staging

or

./laravel-sync push staging

The following options are also available

-a, --ignore-database  Ignore database sync
-d, --ignore-assets    Ignore assets sync
-k, --use-docker       Use docker for database sync
-s, --sendbox          Show only the commands without executing
-y, --yes              Skip confirmation
-c, --no-cleanup       Skip cleanup
-h, --help             Show this help

Run the help command to see the list of available examples:

./laravel-sync -h



πŸ’¬ FAQs

How to pull the remote environment to a local environment that use Sail?

You can use the --use-docker option to use Docker to pull the database. For example:

./laravel-sync pull staging --use-docker

# ... or using the short flag version

./laravel-sync pull staging -k

How can avoid to run SSH command with plain text password?

You can edit the .env file and add set the: SYNC_ENVIRONMENT_<environment_choosed>_DB_PASSWORD to empty.

When you launch the commands, the script will ask you to insert the password.



πŸ‘¨β€πŸ’» Development

Clone the repository and start to develop the script.

Make sure to run use shellcheck to lint the script.

shellcheck laravel-sync



πŸ“ Changelog

Please see CHANGELOG for more information on what has changed recently.



πŸ“ License

The script is open-sourced software licensed under the MIT license.

About

A bash script for pull/push content between environments

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Languages