Back up the database, delete a post and restore it

Description: Create a post Important post. Make a database backup, delete the post for ever, and then restore the backup. Show the number of posts before the delete, after the delete and after the restore.

Hint:

  • wp db export file.sql and wp db import file.sql.
  • wp post list --format=count gives the number of posts.

Solution

Terminal output

$ wp post create –post_title="Important post" –post_status=publish –porcelain
5
$ wp post list –post_type=post –format=count
2
$ wp db export backup.sql
Success: Exported to 'backup.sql'.
$ wp post delete 5 –force
Success: Deleted post 5.
$ wp post list –post_type=post –format=count
1
$ wp db import backup.sql
Success: Imported from 'backup.sql'.
$ wp post list –post_type=post –format=count
2

How it works

  • The export writes all tables as SQL commands in one file.
  • The import runs the file again and brings the database back to the moment of the backup.
  • Keep the backup file outside the web folder on a real site, and copy wp-content too.

Read Backup and Update.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.