Skip to content

Instantly share code, notes, and snippets.

@WarpedOne
Forked from wizonesolutions/HOST_import
Created October 17, 2019 23:26
Show Gist options
  • Select an option

  • Save WarpedOne/e1d510f42eeba59b6c7aa7a42ebf417a to your computer and use it in GitHub Desktop.

Select an option

Save WarpedOne/e1d510f42eeba59b6c7aa7a42ebf417a to your computer and use it in GitHub Desktop.

Revisions

  1. @wizonesolutions wizonesolutions revised this gist Oct 17, 2019. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
  2. @wizonesolutions wizonesolutions revised this gist Oct 17, 2019. 2 changed files with 47 additions and 7 deletions.
    29 changes: 29 additions & 0 deletions advanced-container-import
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    #!/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
    25 changes: 18 additions & 7 deletions import-db → import
    Original file line number Diff line number Diff line change
    @@ -2,14 +2,15 @@
    DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
    prefix=$1
    filename=$2
    target_dir="$DIR"/../custom-import-"$prefix"
    target_dir_name=custom-import-"$prefix"
    target_dir="$DIR"/../../"$target_dir_name"

    print_usage() {
    printf "Usage: ddev import <prefix> <path to .sql or .sq.gz file>"
    echo "Usage: ddev import <prefix> <path to .sql or .sq.gz file>"
    }

    # shellcheck source=../commands/web/SHARED_functions
    . "$DIR"/../commands/web/SHARED_functions
    . "$DIR"/../web/SHARED_functions
    alias=$(get_alias "$prefix") || {
    rc=$?
    printf "\n"
    @@ -18,7 +19,7 @@ alias=$(get_alias "$prefix") || {
    }

    [ -f "$filename" ] || {
    printf "Missing filename."
    echo "Missing filename."
    print_usage
    exit 2
    }
    @@ -29,17 +30,27 @@ alias=$(get_alias "$prefix") || {
    echo "Ensuring the container can see the dump..."
    rm -rf "$target_dir"
    mkdir -p "$target_dir"
    target="$target_dir/dump"
    target_name="dump"
    target="$target_dir/$target_name"
    cp "$filename" "$target" || {
    printf "Copying the file failed for some reason. Can your user read the file you are trying to copy? Try copying it
    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" "$target"
    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!"
  3. @wizonesolutions wizonesolutions created this gist Oct 17, 2019.
    51 changes: 51 additions & 0 deletions import-db
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    #!/bin/bash
    DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
    prefix=$1
    filename=$2
    target_dir="$DIR"/../custom-import-"$prefix"

    print_usage() {
    printf "Usage: ddev import <prefix> <path to .sql or .sq.gz file>"
    }

    # shellcheck source=../commands/web/SHARED_functions
    . "$DIR"/../commands/web/SHARED_functions
    alias=$(get_alias "$prefix") || {
    rc=$?
    printf "\n"
    print_usage
    exit $rc
    }

    [ -f "$filename" ] || {
    printf "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="$target_dir/dump"
    cp "$filename" "$target" || {
    printf "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
    }
    echo "done."

    # Call the container script now that we know where the file is.
    ddev advanced-container-import "$alias" "$target"

    echo "
    Import complete!"

    finish() {
    # Your cleanup code here
    rm -rf "$DIR"/../custom-import-"$prefix"
    }
    trap finish EXIT