|
Databases

How to back up a database properly

A green success message is not a backup. Mine was a 40MB file for a 400MB database, and a client lost a month of orders. Here is how to do it properly.

How to back up a database properly

In 2019 a client's host migrated her account and dropped the wrong database. Her backup was a phpMyAdmin export from three weeks earlier, and it had timed out halfway, so the file was cut off mid table. She lost every order from the busiest month of the year.

I was the one who had set up that backup. I had watched the export finish and assumed the green message meant it worked. It did not. The file was 40MB when the database needed 400MB, and nobody opened it to check.

That is why I am annoying about backups now.

A backup you have not restored is a hope

Everyone has a backup. Very few have a restore. The difference only shows up on the worst day, and by then it is too late to learn.

A real backup is a file you have taken, moved somewhere else, and successfully loaded into an empty database while checking that the row counts match. Until you have done that, you have a file. Maybe.

So the first rule is not about tools. It is about proof. Schedule a restore, not just a dump.

A database is not just tables. It is the schema, the data, the users and permissions, and sometimes stored procedures and triggers. A dump that skips routines will restore an empty shell that looks fine until a button does nothing.

Write down what lives in your database before you back it up. If the app also stores uploaded images on disk, those need their own copy. A database backup with missing images is half a backup, and the missing half is the part customers see.

The two commands worth memorising

For MySQL and MariaDB:

mysqldump -u user -p -R mydb > mydb-2026-07-09.sql

For PostgreSQL:

pg_dump -U user -Fc mydb > mydb-2026-07-09.dump

The -R flag includes stored routines, which the default dump skips. On a live database, also add the single transaction option: it starts a consistent snapshot, so you do not capture a half written state where an order exists but its items do not. The -Fc flag on Postgres gives you a compressed custom format that restores faster and lets you restore a single table if you need to.

Do not use phpMyAdmin's export button for anything past a few hundred megabytes. It runs inside a browser request, the request times out, and you get a partial file with a cheerful success screen. I have seen this happen at least five times. Use the command line.

Three copies, two places, one off site

Keep the live database, a local backup, and one copy somewhere else. The local one is for fast restores. The off site one is for the day the server is gone, the host locks you out, or someone runs a bad DELETE.

Google Drive is fine for a small business. A nightly dump uploaded to a folder, with the date in the filename, gives you 30 days of history for free. Just make sure the upload completes and the file size looks sane compared to yesterday's.

If the business can afford it, use object storage with versioning instead. Drive sync can be paused, and a paused sync is a silent failure. Whatever you choose, check the destination, not just the source.

Automate it and make it complain

A backup that depends on you remembering is not a backup. Put it in cron or a scheduled job. Then make it tell you when it fails.

  1. Run the dump at a quiet hour, around 2am.
  2. Check the exit code. If it is not zero, send an email or a WhatsApp message.
  3. Check the file size against yesterday. A sudden drop means a partial dump.
  4. Upload it off site and confirm the upload returned success.
  5. Delete dumps older than 30 days, but never the first of the month.

That last line has saved me twice. Monthly archives are small and they let you go back far enough to notice a data problem that has been growing quietly for weeks.

Test the restore, not the dump

Once a quarter, take the newest backup, load it into a separate database, and run three counts: customers, orders, order items. Compare them to production. If the numbers are close, you have a backup. If the restore errors halfway, you have found the problem on a quiet Tuesday instead of a bad Friday.

I keep this as a checklist in the repo now. It takes 20 minutes and it has caught a wrong flag, a missing routine, and once a disk that was silently full.

Restore one today

Take a dump of your current database right now. Load it into a database with a different name. Count the rows in your three biggest tables and compare. If that works, put the dump on a schedule and upload it somewhere that is not the same server.

If you are still setting up the schema, read how to design a database for a small business app so the restore has something worth restoring. And if your database is already slow, fix that before the backup window gets longer, starting with why your database gets slow. The software development course covers backup and recovery alongside the day to day work.

Dave Partner

Dave teaches digital skills at Braintem and has helped thousands of Nigerians move from watching tutorials to charging for real work. He writes about the parts nobody puts in the course trailer.