Skip to content

Instantly share code, notes, and snippets.

@PKuhlmay
Created March 4, 2024 16:21
Show Gist options
  • Save PKuhlmay/edfaf1d0392d5e42dcd741f1f10f810b to your computer and use it in GitHub Desktop.
Save PKuhlmay/edfaf1d0392d5e42dcd741f1f10f810b to your computer and use it in GitHub Desktop.
Command for importing external database into own DDEV project
#!/usr/bin/env bash
## Description: Imports the database from the remote host
## Usage: importdatabase
## Example: "ddev importdatabase"
#!/bin/bash
# read .env file
if [ -f .env ]; then
export $(cat .env | xargs)
else
echo ".env file is missing"
exit 1
fi
# Obtain confirmation of the user
read -p "Do you really want to override the local database? (yes/no) " confirmation
if [[ $confirmation != "yes" ]]; then
echo "Operation cancelled."
exit 1
fi
# Pull database from remote host
echo "Pulling database from remote host ..."
mysqldump -h $REMOTE_DB_HOST -u $REMOTE_DB_USER -p$REMOTE_DB_PASSWORD $REMOTE_DB_NAME --verbose > remote_db.sql
# Check if the database dump was created successfully
if [ $? -ne 0 ]; then
echo "Error: Database dump could not be created."
exit 1
fi
# Import database into DDEV
echo "Importing database into DDEV ..."
ddev import-db --file=remote_db.sql
echo "Database imported."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment