Skip to content

Instantly share code, notes, and snippets.

@LostinOrchid
Created July 6, 2018 04:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save LostinOrchid/bc33859903d0d2b1f9d4c193df31649e to your computer and use it in GitHub Desktop.
Save LostinOrchid/bc33859903d0d2b1f9d4c193df31649e to your computer and use it in GitHub Desktop.
Create database backup in Wordpress
#!/usr/bin/env bash
sqllocation=~/.sql_backup_files
# echo $(wp --version)
wp --version >/dev/null 2>&1 || {
echo "I need 'wp' command to be able to execute further"
exit 1
}
# Check if it is a wordpress directory or is installed
wp config list >/dev/null 2>&1 || {
echo "$(pwd) is not a valid wordpress site, or wordpress is not installed."
exit;
}
generate_filename()
{
echo $(wp config get DB_NAME)-$(date "+%F-%X" | sed "s/:/-/g").sql
}
run_command()
{
echo "Running command: $1"
$1
}
if [[ ! -d $sqllocation ]];
then
echo "Creating directory: $sqllocation"
run_command "mkdir -p $sqllocation"
fi
if [[ ! -z $1 ]];
then
sqlfilename=$(sed "s/.sql//g" <<< $1).sql
else
sqlfilename=$(generate_filename)
fi
fullname=$sqllocation/$sqlfilename
if [[ -f $fullname ]];
then
read -p "$fullname exists: Overwrite?[y/n]: " overwrite
if [[ $overwrite = "y" ]];
then
run_command "wp db export $fullname"
fi
else
run_command "wp db export $fullname"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment