Single Table Backup
March 31, 2025 ยท AI Conversations
—
### ๐น Using `mysq
—
## ๐น Backup a Single Table Using `mysqldump`
mysqldump -u [username] -p [database_name] [table_name] > [table_name].sql
### Example:
mysqldump -u root -p my_database users > users.sql
This will create a backup of the `users` table into a file called `users.sql`.
—
## ๐น Import the Table Backup Using `mysql`
To import the table into the same or another database:
mysql -u [username] -p [database_name] < [table_name].sql
### Example:
mysql -u root -p my_database < users.sql
> โ If the table already exists, you may want to drop it first (or truncate it if you're keeping the structure).
—
## ๐ Tip: Selective Table Export with Conditions (Optional)
You can also dump specific rows using a `WHERE` clause:
mysqldump -u root -p my_database users --where="created_at >= '2025-01-01'" > users_partial.sql
Let me know if you're using **phpMyAdmin**, **Laravel DB commands**, or a **Docker setup**, and I can tailor the steps.
—
### ๐น 1. Copy Table Structure + Data
CREATE TABLE new_table_name AS SELECT * FROM original_table_name;
### Example:
CREATE TABLE users_backup AS SELECT * FROM users;
—
### ๐น 2. Copy Only Table Structure (No Data)
CREATE TABLE new_table_name LIKE original_table_name;
### Example:
CREATE TABLE users_structure_only LIKE users;
Then you can optionally insert data later:
INSERT INTO users_structure_only SELECT * FROM users WHERE status = 'active';
—
### ๐น Run These from CLI (if you're using `mysql` command):
mysql -u root -p -e "CREATE TABLE users_backup AS SELECT * FROM users;" my_database
Change `root`, `my_database`, `users`, and `users_backup` as needed.
—
Let me know if you want to copy **only certain columns or rows**, or if you're using a specific interface like **phpMyAdmin**, **Laravel**, or **Docker**.