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.sqlandwp db import file.sql.wp post list --format=countgives 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-contenttoo.
Read Backup and Update.
Learn more about the similar topics:
| Tutorials |
|---|
| WordPress Database |
| WordPress Backup and Update |
| WordPress $wpdb Class |
| Exercises & Assignments |
|---|
| Back up the database, delete a post and restore it |
| Create a custom table and insert a row with $wpdb |
| Interview Questions & Answers |
|---|
| How many default tables are in a WordPress database? |
| Where is the WordPress content stored? |
| What is the default table prefix for the WordPress database? |