Monday, 28 September 2015

Backup Wordpress Posts using BackWPUp and Python [How To]

Want to keep a copy of all your wordpress posts in your own computer? This post talks about how to backup wordpress posts and store these posts in separate files.

Backup Wordpress Posts using BackWPUp and Python [How To]



There's a plugin called BackWPUp that downloads all the information related to your blog to your local computer. This information is stored in a .sql file, which can then be processed using a simple Python script and all the posts can be stored in separate files. So let's begin.

=>Login to your wordpress account. Click on BackWPUp on the left menu. Then, click on Download Database Backup. Then save the file. The size of this size may vary, based on the amount of posts, the amount of content in each post etc.

Backup Wordpress Posts

=>Let us call the downloaded file "blog_sugeek.sql". This file contains information about the actual database that contains all the information about the posts.

=>Open up your terminal. Browse to the folder where the downloaded .sql file is placed using cd (change directory). Then type:
mysql --user=root --password=root

The username and password may be different on your machine.

=>Create a new database by typing:
create database if not exists myblog;

=>Now, we need this myblog database to store the information about our blog. To do so, type:
use myblog
source blog_sugeek.net;

Screenshot from 2015-09-26 14:25:29

=>To get the details about what all values are stored in this database, type:
show tables;

=>The table that we are interested in right now is the "wp_posts" table. To know about the names of columns stored in it, type:
show columns from wp_posts;

Screenshot from 2015-09-26 14:30:13

=>Type \q to exit. Then, create a new file named "getTitleContent.py" and enter the following lines of code:
import MySQLdb

connection = MySQLdb.connect('localhost','root','root','myblog')
cursor = connection.cursor()

cursor.execute('use myblog')
cursor.execute("select post_name,post_content from wp_posts")

data = cursor.fetchall()

for row in data:
name,content = row
if name:
f = open(name,'w')
f.write(content)
f.close()
connection.close()


=>Save the file. Now run it by typing:
python getTitleContent.py

=>That's it! Now type ls to check that all the posts are stored in separate files.

Screenshot from 2015-09-26 14:39:01

Was this post helpful? Please let us know in the comments section!

No comments:

Post a Comment