Skip to content

Instantly share code, notes, and snippets.

@themkvz
Last active Mar 11, 2022
Embed
What would you like to do?
#!/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 si
te 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 OLD WordPress directory !!!
"
usage;
fi
rm -rf "$wp_src"
echo "Old files removed!"
pwd=`pwd`
cd "$bcp_dst"
tar -xf "$bcp_name"_"$file_suf".tar.gz -C "$pwd"
cd "$pwd"
echo "Backup files restored!"
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"
cd "$bcp_dst"
mysqladmin -u$db_user -p$db_pass drop -f $db_name
echo "Dont worry!"
mysqladmin -u$db_user -p$db_pass create -f $db_name
echo "Database \"$db_name\" again created"
gunzip < "$bcp_name"_"$db_suf".sql.gz | mysql -u$db_user -p$db_pass $db_name
echo "Database restored!"
cd "$pwd"
# mysqldump -u$db_user -p$db_pass $db_name|gzip > "$bcp_dst"/"$db_name"_`date +%Y%m%d_%H%M`".sql.gz"
# mysqldump -u$db_user -p$db_pass $db_name|gzip > "$bcp_dst"/"$bcp_name"_db_backup.sql.gz
# wp_domain=`mysql -u$db_user -p$db_pass $db_name -s -N -e "select option_value from $db_opts where option_name='home';"|awk -F \/ '{print $3}'`
# backup files
# wp_dst="$bcp_dst"/"$wp_domain"_`date +%y%m%d_%H%M`
# tar -czvf "$bcp_dst"/"$bcp_name"_file_backup.tar.gz -P "$wp_src"
echo "All complete!"
# 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