I couldn’t find any receipt to clone a WordPress blog. A user needed to have a test copy of his blog.

Here is mine. I hope it can be useful to others.

Copy the WP:

cd /path/to/myblog
cd ..
cp -a myblog test-myblog
cd test-myblog
vi wp-config.php # adapt DB_NAME DB_USER DB_PASSWORD
for f in `grep myblog . -rl` ; do perl -pi~ -e 's/myblog/test-myblog/g' $f; done

Prepare the DB for the clone:

mysqldump -u root -p -B blog_myblog > blog_myblog.dump
vi blog_myblog.dump
# :%s/blog_myblog/blog_test_myblog/g
# :%s/myblog/test-myblog/g
perl -pE 's/(s:(\d+?):\\"([^"]*?test-myblog[^"]*?)\\")/ "s:" . (${2} + 5) . ":\\\"$3\\\"" /eg' blog_myblog.dump > blog_test_myblog.dump
# NOTE: 5 is the character difference between "myblog" and "test-myblog"

This is the magic here. Because WordPress stores string lengths along with strings in the DB.

We need to adapt them in the dump to be imported into the new DB for the clone. Otherwise, the clone won’t be fully functional.

Of course, a quicker alternative is to chose a name with the same length.

Create the database for the clone:

mysql -u root -p < blog_test_myblog.dump
mysql -u root -p
CREATE user "myuser"@"localhost";
SET password FOR "myuser"@"localhost" = password("secret");
GRANT ALL ON blog_test_myblog.* TO "myuser"@"localhost";

Adapt the apache conf:

vi /etc/apache2/sites-available/myblog
/etc/init.d/apache2 reload