Skip to content

Instantly share code, notes, and snippets.

@jonasbjork
Created August 8, 2017 12:16
Show Gist options
  • Select an option

  • Save jonasbjork/785748fadfb9d9fa832bf4ba541d4290 to your computer and use it in GitHub Desktop.

Select an option

Save jonasbjork/785748fadfb9d9fa832bf4ba541d4290 to your computer and use it in GitHub Desktop.
Export WordPress blog from MultiSite installation and archive it to a tar.gz
#!/usr/bin/env bash
DBHOST="localhost"
DBUSER="DATABASE_USER"
DBPASS="DATABASE_PASS"
DBBASE="DATABASE_NAME"
WPFILES="/var/www/wp-content/uploads/sites"
if [ -z ${1} ]; then
echo "You must specify blog ID, i.e. 123"
exit 1
fi
DOMAINNAME=$(mysql -h ${DBHOST} -u ${DBUSER} -p${DBPASS} ${DBBASE} -e "select domain from wp_blogs where blog_id=${1} \G" | grep ^domain | awk '{print $2}')
echo "Domain : ${DOMAINNAME}"
TABLES=$(mysql -h ${DBHOST} -u ${DBUSER} -p${DBPASS} ${DBBASE} -e "show tables like '%_${1}_%' \G" | grep ^Tables_in | awk '{print $3}')
if [ -z "${TABLES}" ] ; then
echo "No tables found for ${1}"
exit 1
fi
mkdir -p ${DOMAINNAME}/{sql,files}
for t in ${TABLES}; do
mysqldump -h ${DBHOST} -u ${DBUSER} -p${DBPASS} ${DBBASE} ${t} > ${DOMAINNAME}/sql/${t}.sql
done
cp -R ${WPFILES}/${1} ${DOMAINNAME}/files/
echo "SQL-dump: ${DOMAINNAME}/sql/"
echo "WP-files: ${DOMAINNAME}/files/"
tar zcf ${DOMAINNAME}.tar.gz ${DOMAINNAME}/
echo "---------------------------------------------------"
echo "Archive : ${DOMAINNAME}.tar.gz"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment