Skip to content

Instantly share code, notes, and snippets.

@mariuswilms
Last active August 29, 2015 14:07
Show Gist options
  • Save mariuswilms/21c874c8a8e1785fb17e to your computer and use it in GitHub Desktop.
Save mariuswilms/21c874c8a8e1785fb17e to your computer and use it in GitHub Desktop.
Migration scripts to change URLs in blog posts from lithify.me to li3.me
#!/bin/bash
# Fill in your connection details.
DATABASE="blog"
COLLECTION="posts"
USER=""
PASS=""
#
AUTH=""
if [[ $USER != "" ]]; then
AUTH+="-u $USER"
fi
if [[ $PASS != "" ]]; then
AUTH+="-p $PASS"
fi
# Recommended: Optionally uncomment to create a backup before we're applying changes.
# mongodump -d $DATABASE -c $COLLECTION $AUTH
SCRIPT="
var cursor = db.${COLLECTION}.find();
while (cursor.hasNext()) {
var item = cursor.next();
print('Before.. ' + item.body);
item.body = item.body.replace(/lithify.me/g, 'li3.me');
print('After... ' + item.body);
db.${COLLECTION}.update({_id : item._id}, item);
}
"
echo $SCRIPT > script.js
mongo $DATABASE $AUTH ./script.js
# Cleanup
rm script.js
#!/bin/bash
# Fill in your connection details.
DATABASE="blog"
USER="root"
PASS=""
#
AUTH=""
if [[ $USER != "" ]]; then
AUTH+="-u $USER"
fi
if [[ $PASS != "" ]]; then
AUTH+="-p$PASS"
fi
# Recommended: Optionally uncomment to create a backup before we're applying changes.
# mysqldump --opt $AUTH $DATABASE > backup.sql
SCRIPT="
UPDATE wp_posts SET post_content = REPLACE ( post_content, 'lithify.me', 'li3.me');
"
echo $SCRIPT | mysql $AUTH $DATABASE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment