-
-
Save sobujbd/edc159f7f74c4e111c7b0f87708fe1b2 to your computer and use it in GitHub Desktop.
Create database backup in Wordpress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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