Skip to content

Instantly share code, notes, and snippets.

@themkvz
Last active March 11, 2022 15:17
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save themkvz/ec1c24f6f369d91a09bd2cde6977c8d9 to your computer and use it in GitHub Desktop.
Usage: 1. chmod +x backup-wp.sh 2. ./backup-wp.sh public_html/ backup/ first OR curl -sL https://gist.github.com/themkvz/ec1c24f6f369d91a09bd2cde6977c8d9/raw/backup-wp.sh | bash -s public_html/ backup/ first
#!/bin/bash
function usage {
echo "Usage:
$0 [wordpress_site] [backup_path] [name]
e.g.:
bash backup-wp.sh public_html backup name
wordpress_site - path to your WordPress site for backup it (e.g. /home/admin/web/wordpress.com/public_html )
backup_path - destination for your backups (e.g. /home/admin/web/wordpress.com/backup
name - backup name )
";
exit 1
}
if [ $# -lt 3 ]; then usage; fi
wp_src=${1%/}
bcp_dst=${2%/}
bcp_name=${3%/}
file_suf="file_backup"
db_suf="db_backup"
wp_conf="$wp_src"/wp-config.php
if [ ! -f "$wp_conf" ]; then
echo "
$wp_src is not wordpress core directory !!!
"
usage;
fi
db_pass=`cat "$wp_src"/wp-config.php|grep DB_PASSWORD|awk -F \' '{print $4}'`
db_name=`cat "$wp_src"/wp-config.php|grep DB_NAME|awk -F \' '{print $4}'`
db_user=`cat "$wp_src"/wp-config.php|grep DB_USER|awk -F \' '{print $4}'`
db_pref=`cat "$wp_src"/wp-config.php|grep table_prefix|awk -F \' '{print $2}'`
db_opts="$db_pref"options
echo "Get database name:" "$db_name"
echo "Get database user:" "$db_user"
echo "Get database psw:" "***"
# backup database
mkdir -p "$bcp_dst"
mysqldump -u$db_user -p$db_pass $db_name|gzip > "$bcp_dst"/"$bcp_name"_"$db_suf".sql.gz
echo "Database \"$db_name\" dumped"
# backup files
tar -czf "$bcp_dst"/"$bcp_name"_"$file_suf".tar.gz -P "$wp_src"
echo "Files backup"
echo "Backup compled"
ls -lh "$bcp_dst"
du -sh "$bcp_dst"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment