Last Updated: February 25, 2016
·
1.025K
· aaronott

Quick sql-dump with drush and bash aliases

Add the following to your .bashrc or .bash_aliases file

function sql-dump() {
  # Change this to put the output file in a different location
  # here it is being placed in the users home directory (assumes *nix style)
  outpath='~'

  date=`date +"%Y%m%d"`
  path=`drush $1 dd`

  if [ "$path" ] ; then
    arr=$(echo $path | tr "/" "\n")

    for x in $arr
    do
      name=$x
    done

    sql=$date.$name.sql
    outfile=$outpath/$sql
    echo "Dumping the db to $outfile"
   `drush $1 sql-dump --result-file=$outfile --gzip --ordered-dump`

  fi
}

This gives you the ability to go into a Drupal doc root and simply type sql-dump and have the output sent to your home directory with the current date and the sitename (set by the directory path).

You can also take advantage of Drush aliasing here by typing (from anywhere) sql-dump @sitename