Skip to content

Instantly share code, notes, and snippets.

@lrkwz
Last active August 3, 2017 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lrkwz/7237c49b3ee4bf4f19801f24034c8593 to your computer and use it in GitHub Desktop.
Save lrkwz/7237c49b3ee4bf4f19801f24034c8593 to your computer and use it in GitHub Desktop.
Estract a tar of images used by wordpress
#!/bin/bash
set -e
if [ "$1" != "" ]; then
FNAME=$(mktemp /tmp/foo.XXXXXXXXX)
cat <<EOSQL | mysql -u root -p $1 | sed -e s/\.[^\.]*$/\*/ | sed -e "s/^/ls /" > $FNAME
SELECT distinct files.meta_value AS ''
FROM
wp_posts posts
INNER JOIN wp_posts attachments ON posts.ID = attachments.post_parent
INNER JOIN wp_postmeta files ON attachments.ID = files.post_id
WHERE files.meta_key = '_wp_attached_file'
UNION
SELECT distinct REGEXP_REPLACE( guid, '^htt.*\/uploads\/','')
FROM wp_posts attachments
INNER JOIN wp_postmeta files ON attachments.ID = files.post_id
WHERE post_mime_type LIKE 'image%';
EOSQL
CURDIR=$(basename `pwd`)
if [ "$CURDIR" == "uploads" ];then
TARLIST=$(mktemp /tmp/tar.XXXXXXXXX)
sh $FNAME > $TARLIST
tar -c -f /tmp/$1.tar -T $TARLIST
echo "Trovi le immagini di $1 in /tmp/$1.tar"
else
echo "cd to $1/wp-content/uploads dir first"
fi
else
echo "$0 dbname"
fi
@lrkwz
Copy link
Author

lrkwz commented Aug 3, 2017

This can be useful to extract a tar of used images in a given wordpress site.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment