Labs · Cyber Security
Lab: Back Up and Restore a MySQL Database with mysqldump, Then Remove an Unused User
Course: Cyber Security · Chapter 11: MySQL Part 2: UPDATE, ALTER, DELETE, Keys, Constraints and Users
Chapter 11 covers UPDATE, DELETE, keys and users; this lab adds backup, restore and user clean-up.
Chala mitrano! A backup that was never restored is only a hope. Today we take a backup, delete data on purpose, and bring it back. Then we clean up an old user, because every unused account is a door someone can open. Bharpur shikayla milel!
चला मित्रांनो! कधीच restore न केलेला backup म्हणजे फक्त आशा. आज आपण backup घेणार, मुद्दाम data delete करणार, आणि तो परत आणणार. मग एक जुना user काढून टाकणार, कारण प्रत्येक न वापरलेलं account म्हणजे कोणीतरी उघडू शकेल असा दरवाजा. भरपूर शिकायला मिळेल!
चलो दोस्तों! जो backup कभी restore नहीं किया, वो सिर्फ एक उम्मीद है। आज हम backup लेंगे, जानबूझकर data delete करेंगे, और उसे वापस लाएँगे। फिर एक पुराना user हटाएँगे, क्योंकि हर बेकार account एक दरवाज़ा है जिसे कोई खोल सकता है। बहुत कुछ सीखने को मिलेगा!
Suppose we are…
Suppose we manage the database for a Razorpay-style payments demo app in our training batch. A teammate runs DELETE FROM products; without a WHERE and every row is gone. If we have a fresh backup and we have practised restoring it, this is a 2-minute fix. If not, it is a very bad day. We also remove the old report user, because the dashboard project has ended.
Goal of this lab
By the end you will have:
- A backup file
shop-backup.sqlmade with mysqldump, readable only by you. - Restored the
shopdatabase after a deliberate mistake and checked the rows. - Listed all database users and dropped one you no longer need.
What you need (all free)
- Your EC2 server with MariaDB and the
shopdatabase from Lab 10. (No server? Do Part 1 and 2 of Lab 10 first.) - 30–35 minutes.
Safety and ethics
Practise on the shop practice database only. Backups contain all your data: keep them private (chmod 600), never in the website folder, and never in a public Git repository.
Steps
-
SSH to the server and count the rows now:
sudo mariadb -e "SELECT COUNT(*) FROM shop.products;"What you should see:
5(or more, if you added rows). -
Make a folder only you can open, and take the backup:
mkdir -p ~/db-backups && chmod 700 ~/db-backups sudo mysqldump --databases shop --single-transaction > ~/db-backups/shop-backup.sql chmod 600 ~/db-backups/shop-backup.sql ls -l ~/db-backups--databases shopadds the CREATE DATABASE line, and--single-transactiongives a consistent copy without locking the tables.What you should see:
-rw------- 1 ec2-user ec2-user ... shop-backup.sql. -
Look inside the backup; it is plain SQL text:
grep -E "CREATE TABLE|INSERT INTO" ~/db-backups/shop-backup.sqlWhat you should see: a
CREATE TABLEline forproductsand oneINSERT INTOline that holds all 5 rows, starting with(1,'Kurti',499). -
Now make the mistake on purpose:
sudo mariadb -e "DELETE FROM shop.products; SELECT COUNT(*) FROM shop.products;"What you should see:
0. All rows are gone. -
Restore from the backup:
sudo mariadb < ~/db-backups/shop-backup.sql sudo mariadb -e "SELECT * FROM shop.products;"What you should see: all 5 rows back, with the same ids, names and prices.
-
Compress the backup to save space, and check it can still be read:
gzip ~/db-backups/shop-backup.sql zcat ~/db-backups/shop-backup.sql.gz | head -n 20 -
List every database user:
sudo mariadb -e "SELECT user, host FROM mysql.user;"What you should see:
root,mariadb.sys,mysql(system accounts) andreportfrom Lab 10. -
The dashboard project has ended, so remove
report:sudo mariadb -e "DROP USER 'report'@'localhost'; SELECT user, host FROM mysql.user;"What you should see:
reportis no longer in the list. Do not droproot,mariadb.sysormysql.
Ravindra Bagale's Tip
Keep at least one copy of the backup off the server too. If the server disk fails or the server is deleted, a backup on the same disk goes with it. Download it with scp to your laptop or copy it to a private S3 bucket. 3-2-1 rule lakshat theva!
Ravindra Bagale's Tip – मराठी
Backup ची किमान एक copy server च्या बाहेर पण ठेवा. Server ची disk खराब झाली किंवा server delete झाला, तर त्याच disk वरचा backup पण जातो. तो scp ने laptop वर download करा किंवा private S3 bucket मध्ये copy करा. 3-2-1 rule लक्षात ठेवा!
Ravindra Bagale's Tip – हिंदी
Backup की कम से कम एक copy server के बाहर भी रखो। अगर server की disk खराब हो या server delete हो जाए, तो उसी disk वाला backup भी चला जाता है। उसे scp से laptop पर download करो या private S3 bucket में copy करो। 3-2-1 rule याद रखो!
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
Backup saved in /usr/share/nginx/html |
Anyone can download your whole database from the website | Keep backups in a private folder with chmod 600 |
| Never testing a restore | You find out the backup is broken on the bad day | Restore to a test database every month |
mysqldump shop without --databases and then mariadb < file |
No database selected error |
Use --databases shop, or restore with mariadb shop < file |
| Dropping the system users | The server misbehaves | Drop only users your apps created |
| Backup only on the same server | Lost together with the server | Keep a copy off the server |
Self-check checklist
0 of 5 done
Try-at-home challenge
Write a one-line command you could run every night that makes a dated, compressed backup like shop-2026-10-08.sql.gz. Then copy today's backup to your laptop with scp.
Check your answer
sudo mysqldump --databases shop --single-transaction | gzip > ~/db-backups/shop-$(date +%F).sql.gz && chmod 600 ~/db-backups/shop-$(date +%F).sql.gz
From your laptop: scp -i lab-key.pem ec2-user@<server-ip>:~/db-backups/shop-*.sql.gz . To run it every night, put the first command in a script and add it with crontab -e.
Clean up to avoid charges
If you do not need the server for Chapter 12, terminate it: EC2 → Instances → Instance state → Terminate (delete) instance. Copy any backup you want to keep to your laptop first.
Samjla ka? Backup, break, restore, verify, and remove users you do not need. Aata pudhe jaauya: Chapter 12 points a domain at our server.