Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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