DEV Community

loading...

How to backup only original images of your WordPress site using WP-CLI

castroalves profile image Cadu de Castro Alves ・2 min read

I'm member of a WordPress community on WhatsApp. Today, one member raised the following question:

"I need to create a backup of all images of a website, but only the original ones. I don't need the thumbnails. Does anybody know how to do that?"

If you Google about "how to backup full images on WordPress" or something similar, you will find a lot of posts about backing up images, but not only the original ones.

Then, I've discovered that it's possible to download multiple files at once using wget -i filename.txt. filename.txt file should contain a list of URLs, but each one must be in a separate line.

But how could I get a list of ALL full images of a WordPress site?

WP-CLI to the rescue!

WP-CLI is the command-line interface for WordPress. You can update plugins, configure multisite installations and much more, without using a web browser.

For this task, we need to find a way to list the URLs of all original images and save them into a file.

We can do that by running only one command:

$ wp post list --post_type=attachment --field=guid > wp-content/uploads/images.txt

Let me explain each part of this command:

  1. wp post list is a command to list all WordPress posts
  2. --post_type=attachment is an argument to retrieve only images
  3. --field=guid is an argumento to retrieve the URL of each image
  4. > wp-content/upload/images.txt is a command to output the result of wp post list --post_type=attachment --field=guid into a file.

How to download the images (and create a backup)

After running the command above, you should have a file located at https://yousite.com/wp-content/uploads/images.txt.

Now, you should go back to your terminal, cd to the folder you want to download the files, and type the following command:

$ wget -i https://yousite.com/wp-content/uploads/images.txt

As soon as you hit Enter/Return, wget you start to download all the images contained in images.txt file to the computer (or server) you're running this command.

Voila!

Discussion

pic
Editor guide