Skip to content

Instantly share code, notes, and snippets.

@wizonesolutions
Last active October 17, 2019 23:26
Show Gist options
  • Save wizonesolutions/ad8ae28ed1f3cc9cdef498c3d5145b64 to your computer and use it in GitHub Desktop.
Save wizonesolutions/ad8ae28ed1f3cc9cdef498c3d5145b64 to your computer and use it in GitHub Desktop.
#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
prefix=$1
filename=$2
target_dir_name=custom-import-"$prefix"
target_dir="$DIR"/../../"$target_dir_name"
print_usage() {
echo "Usage: ddev import <prefix> <path to .sql or .sq.gz file>"
}
# shellcheck source=../commands/web/SHARED_functions
. "$DIR"/../web/SHARED_functions
alias=$(get_alias "$prefix") || {
rc=$?
printf "\n"
print_usage
exit $rc
}
[ -f "$filename" ] || {
echo "Missing filename."
print_usage
exit 2
}
# Copy the filename to somewhere we know is accessible from the container.
# Segment it by prefix so we don't wipe out a dump in the middle of getting
# imported if they run multiple copies of this script.
echo "Ensuring the container can see the dump..."
rm -rf "$target_dir"
mkdir -p "$target_dir"
target_name="dump"
target="$target_dir/$target_name"
cp "$filename" "$target" || {
echo "Copying the file failed for some reason. Can your user read the file you are trying to copy? Try copying it
somewhere manually (with 'cp'), and when you've figured out the problem, run this script again."
print_usage
exit 3
}
# gzip won't gzip already-gzipped files, so we do this to make sure zcat will
# work later.
gzip "$target"
# If file wasn't renamed by gzip, it was already gzipped, so do it ourselves.
if [ -f "$target" ]; then mv "$target" "$target".gz; fi
target_name="$target_name".gz
echo "done."
# Call the container script now that we know where the file is.
ddev advanced-container-import "$alias" /var/www/html/.ddev/"$target_dir_name"/"$target_name" || {
echo "Container import failed! Please check the output and try again."
exit 4
}
echo "
Import complete!"
finish() {
# Your cleanup code here
rm -rf "$DIR"/../custom-import-"$prefix"
}
trap finish EXIT
#!/bin/bash
# This script is primarily intended to be called by 'ddev import', but if
# you have a DB dump that is accessible from ddev (it's within your project),
# you can also call this manually. However, it's less user-friendly because it
# requires a relative path to the database dump (tab completion won't work).
alias=$1
filename=$2
print_usage() {
echo "Usage: ddev advanced-container-import <Drush alias, e.g. @ddev.demo> <container path to gzipped file>"
}
[ -n "$alias" ] || {
echo "Missing alias."
print_usage
exit 1
}
# Don't try to get stdin from the user if they haven't piped anything.
[ -f "$filename" ] || {
echo "Can't find passed-in file ($filename)."
print_usage
exit 1
}
echo "Importing..."
drush "$alias" sql-drop
zcat "$filename" | drush "$alias" sqlc && drush "$alias" cr && drush "$alias" -y updb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment