craft-scripts icon indicating copy to clipboard operation
craft-scripts copied to clipboard

Add push_db

Open sjelfull opened this issue 7 years ago • 7 comments

When developing a site locally, I usually push to staging all the time. Do you have a script for pushing to remote already, or should I send you a pull request?

sjelfull avatar Feb 26 '17 12:02 sjelfull

The workflow I use for that is I have Forge set up to auto-deploy when pushed to the staging or develop branch of my Git repo.

So I don't really need a script to do that; if you want to then pull the db from local to staging, you could either set up the config on the staging server to pull the db from your local dev, or yeah, you could make a push_db script analogous to the pull_db script that would do the opposite.

Pull requests would be awesome if you do decide to make a push_db script; it should be quite simple to do by modifying the pull_db script.

khalwat avatar Feb 26 '17 21:02 khalwat

If you do, you might also want to make an analogous push_assets script as well.

khalwat avatar Feb 26 '17 21:02 khalwat

Has anything happened with this? I may be misunderstanding how these scripts should be utilised, but our current setup is production branch connected to master db. All changes to the database are made on the master, then the scripts pulls the db and assets down locally to my local env. Now we have a staging env as well, so ideally I'd like to pull the master db and assets down to this env, or push them up from my local to it as it's essentially the same. Is this currently possible?

shornuk avatar Oct 05 '17 10:10 shornuk

@shornuk you should try using a workflow as described here:

https://nystudio107.com/blog/database-asset-syncing-between-environments-in-craft-cms

You absolutely can pull the db & assets to your staging environment; just set up a .env.sh for that environment as well.

khalwat avatar Oct 06 '17 16:10 khalwat

@sjelfull did you ever work up a workflow for pushing_db & push_assets? 😬

chasegiunta avatar Aug 31 '18 17:08 chasegiunta

Naah, I did not. Someday 😬

sjelfull avatar Sep 07 '18 13:09 sjelfull

@chasegiunta @sjelfull Anyone think this would work for pushing the local database to the remote database? I tried to plug into their scripts and variables as much as possible. This has been tested in pieces, but not as a whole. I'm about 99% sure it should work, but 1% of me still thinks it will crash everything : P

One of my main concerns is that running the remote restore_db.sh script will overwrite local variables with remote variables. I'm still setting up the remote *.env.sh file, so am not sure if this will create issues. Should be able to test for this issue later this week with "echo" statements.

push_db.sh

#!/bin/bash

# Get the directory of the currently executing script
DIR="$(dirname "${BASH_SOURCE[0]}")"

# Include files
INCLUDE_FILES=(
    "common/defaults.sh"
    ".env.sh"
    "common/common_env.sh"
    "common/common_db.sh"
)

for INCLUDE_FILE in "${INCLUDE_FILES[@]}"; do
    if [[ ! -f "${DIR}/${INCLUDE_FILE}" ]] ; then
        echo "File ${DIR}/${INCLUDE_FILE} is missing, aborting."
        exit 1
    fi
    source "${DIR}/${INCLUDE_FILE}"
done

# Backup local database:
./backup_db.sh

# Set the backup db file name, parent directory path, and full path
BACKUP_DB_DIR_PATH="${LOCAL_BACKUPS_PATH}${LOCAL_DB_NAME}/${DB_BACKUP_SUBDIR}/"
BACKUP_DB_NAME="$(basename $(ls -t $BACKUP_DB_DIR_PATH*.sql* | head -1))";
BACKUP_DB_PATH="${BACKUP_DB_DIR_PATH}${BACKUP_DB_NAME}";

# Copy local backup to server:
echo "*** Copying $BACKUP_DB_PATH to $REMOTE_BACKUPS_PATH$BACKUP_DB_NAME";
scp "$BACKUP_DB_PATH" "$REMOTE_SSH_LOGIN:$REMOTE_BACKUPS_PATH$BACKUP_DB_NAME";

# Log into server and restore local backup
ssh -t $REMOTE_SSH_LOGIN << EOF
"${REMOTE_ROOT_PATH}scripts/restore_db.sh" "$REMOTE_BACKUPS_PATH$BACKUP_DB_NAME";
EOF

# Normal exit
exit 0

callaginn avatar Dec 14 '18 07:12 callaginn