• Resolved ohnoezmahblog

    (@ohnoezmahblog)


    Hello,
    On one of the sites I run, a user accidentally deleted all the media from the library. I have a backup of the media files, but no database backup.

    I’ve uploaded the media files to the same locations, is there a way to get the media library to ‘see’ them and restore the links in the post?

    I was looking at this plugin:
    Add From Server

    But that would not restore the original links. Any ideas?

    And yes, I should have been doing backups.

Viewing 4 replies - 1 through 4 (of 4 total)
  • I had this problem a few months back, and I’m sorry to say that there’s no shortcut that I’ve been able to find. If you have enough memory on your server, Add from Server can do a dozen or so at a time – or you can use the WordPress uploader to do multiple files at a time. Then you’ll have to insert them into each post/page manually.

    Wish there was a better way…

    Thread Starter ohnoezmahblog

    (@ohnoezmahblog)

    Okay, I figured out a work-around:

    I uploaded all the files again via the Media Uploader, and that put them in a folder like:
    https://www.yourdomain.com/wp/wp-content/uploads/2013/03/

    So now WordPress knows about the media, it just has the wrong link.

    Then, I went into the phpmyadmin backend, and used SQL commands like:
    UPDATE wp_posts SET post_content = replace(post_content, 'https://www.yourdomain.com/wp/wp-content/uploads/2012/','https://www.yourdomain.com/wp/wp-content/uploads/2013/')

    Which would update all paths that were in the 2012 folder to the same month in the 2013 folder. Once I had done all the years, I did months:
    UPDATE wp_posts SET post_content = replace(post_content, 'https://www.yourdomain.com/wp/wp-content/uploads/2013/01/','https://www.yourdomain.com/wp/wp-content/uploads/2013/03/')
    Running that command for every month. That restored the vast majority of my links.

    Drawbacks:
    1. It doesn’t restore Featured Images.
    2. All of the media is listed as Unattached.

    Also, make sure you backup your database before attempting this. It’s really easy to hit the wrong key and break your database irrevocably.

    Hope it helps someone.

    You rock. ??

    Thread Starter ohnoezmahblog

    (@ohnoezmahblog)

    Thanks.

    I adapted that SQL command from the guide here, if it helps someone else (also the best guide I’ve found on how to move WordPress).

    The other tool that is helpful is Broken Link Checker, because of the vagaries of file naming there will be some links that don’t restore, this will tell you where they are.

    Really, it seems like WP could have some more smarts in this respect.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Restoring database links to media that was deleted’ is closed to new replies.