Skip to content

Instantly share code, notes, and snippets.

@artistro08
Last active March 10, 2021 15:01
Show Gist options
  • Save artistro08/84b20966efa5d92818659b45851d8971 to your computer and use it in GitHub Desktop.
Save artistro08/84b20966efa5d92818659b45851d8971 to your computer and use it in GitHub Desktop.
Sync Storage & Database from Local to Dev - OctoberCMS
#!/bin/bash
# OctoberCMS Deployment for Git managed Files
#
# I created this bash script because I found myself
# Typing the same commands over and over again to
# import my storage and database directories for
# Clients. Running this will (more or less) zip your
# storage directory, Export your database, and import
# your database & storage directory to the server
# Where you have git deployed.
#
# I AM NOT RESPONSIBLE FOR WHAT HAPPENS WHEN YOU
# RUN THIS FILE. ALWAYS BE CAREFUL WHEN RUNNING
# BASH SCRIPTS FROM EXTERNAL SOURCES.
#
# You'll need to create an .env file and put it in
# the root directory of your OctoberCMS install.
# Installation. Fill it with your MySQL Password.
# Luckily, if you use .env variables in your OCS
# Install, you'll already have them there.
#
# This also requires that you have SSH keys setup for
# the server you're deploying to.
# =====================================================
# Get Environment Variables from .env file
if [ -f .env ]
then
export $(cat .env | sed 's/#.*//g' | xargs)
fi
# Stop if there is an error
set -e
# keep track of the last executed command
trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG
# echo an error message if there is an error
trap 'echo "\"${last_command}\" command filed with exit code $?."' SIGHUP SIGINT SIGTERM
# Set the PROJECT variable to the current folder
PROJECT=$(basename $PWD)
# Create The Files Needed to Port
echo "Creating copy of storage..."
tar -czf storage.tar.gz storage/
echo "Creating copy of database..."
mysqldump --add-drop-table -u root $DB_DATABASE > $DB_DATABASE.sql
# Copy the Files to the server
echo "Copying files to the server..."
scp -i ~/.ssh/id_rsa storage.tar.gz user@example.com:/home/user
scp -i ~/.ssh/id_rsa $DB_DATABASE.sql user@example.com:/home/user
# Apply the files to application & cleanup after
echo "Applying files to the server..."
ssh -t user@example.com "mysql -u $DB_DATABASE -p$DB_PASSWORD $DB_DATABASE < $DB_DATABASE.sql | sudo mv ~/storage.tar.gz /var/www/html/$PROJECT && cd /var/www/html/$PROJECT/ && sudo tar --skip-old-files -xzf storage.tar.gz && sudo rm storage.tar.gz && sudo chown -R www-data:www-data storage/ && sudo chmod g+w storage/ -R"
# Cleaning up local files
echo "Cleaning up..."
rm $DB_DATABASE.sql
rm storage.tar.gz
# setting permissions (omit if you don't need to set)
sudo chown -R www-data:www-data /var/www/html/$PROJECT
sudo chmod g+w /var/www/html/$PROJECT -R
# Setup completed successfully
echo "Sync Completed Successfully"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment