MySQL databases change constantly, and a backup taken by hand once in a while is not a backup strategy. A shop order, a new user, a comment posted five minutes ago: all of it lives only in the database, so losing the database means losing work your visitors already did. This mysql backup with mysqldump guide shows how to automate nightly dumps with cron so a fresh dump lands on disk every night without you touching anything.
mysqldump is the command-line tool that ships with MySQL and MariaDB. It reads a database and writes it out as a SQL file you can feed back into a fresh server to rebuild the same data. It is not the fastest tool for very large databases, and it is not the only option, but for the small databases a typical VPS runs, it is simple, reliable, and already installed. Combined with cron, it gives you automatic backups with no extra software to maintain.
MySQL Backup with mysqldump: The Basic Command
The core command is short: mysqldump -u root -p your_database > backup.sql. It asks for the root password, dumps the database, and writes the SQL to a file. Add –single-transaction to the command and the dump stays consistent for InnoDB tables, which is what most WordPress and app databases use. Without it, a dump taken while the site is busy can mix data from different moments in time, and a restore from that dump can leave tables out of sync with each other.
For a full server, dump every database at once with mysqldump –all-databases instead of naming one. That catches databases you forgot existed, including ones created by an app you installed months ago. Keep the dump files in a dedicated folder such as /var/backups/mysql, owned by root and not served by your web server. A backup file sitting in a public web directory is a data breach waiting for a search engine to find it.
Compress the dump as you write it. Database dumps compress well, often to a tenth of their size, because SQL text repeats itself constantly. Pipe the output through gzip, like mysqldump –single-transaction your_database | gzip > backup.sql.gz. On a small VPS with a tight disk, compression is not optional. It is the difference between keeping a month of dumps and keeping three days.
Storing the MySQL Password Safely
A cron job cannot type a password, so the backup script needs credentials that work without interaction. The safe way is a MySQL option file at /root/.my.cnf containing the user and password under a [mysqldump] section, with permissions set to 600 so only root can read it. The script then runs mysqldump without -p, and MySQL reads the credentials from the file. Never put the password on the command line or inside the crontab. Command lines show up in the process list, and anyone who can read the crontab can read the password.
Better still, create a dedicated backup user instead of using root. Give that user only SELECT, SHOW VIEW, TRIGGER, and LOCK TABLES on the databases it backs up, and nothing else. If the credentials ever leak, the damage is limited to reading data, not dropping tables or creating users. The backup user also makes it obvious in the MySQL logs which connections are backups and which are the application, which helps when you are tracing a problem later.
Writing the Automatic MySQL Backup Script
A mysqldump cron backup works best as a small shell script that cron calls, rather than a long command pasted into the crontab. The script sets the date, dumps each database to its own dated file, compresses it, and deletes dumps older than a set number of days. One file per database makes restores simpler: you can restore a single site without touching the others, and a corrupt dump affects one database instead of all of them.
Schedule it for a quiet hour, such as 2 a.m., when the site is least busy. A dump of a small database takes seconds, so the exact time barely matters, but a quiet window avoids competing with nightly updates and other cron jobs for CPU and disk. If you run other nightly work, stagger the times so two heavy jobs never start together on a 1 GB VPS. The guide to monitoring disk, RAM, and CPU usage on a VPS helps you see whether the backup window overlaps with anything expensive.
Scheduling the Backup with Cron
Cron runs the script on a schedule you define. Open the root crontab with crontab -e and add a line such as 0 2 * * * /usr/local/bin/mysql-backup.sh >> /var/log/mysql-backup.log 2>&1. That runs the script daily at 2 a.m. and appends all output to a log file. The log is important: without it, a failing backup is invisible until the day you need it.
Read that log once a week. A healthy log shows a fresh dump file for each database and a size that looks sane. A dump file that suddenly shrinks to a few hundred bytes usually means the dump failed and only an error message got compressed. Check the file sizes with ls -lh and compare them with the previous day. A backup routine you never look at is a routine you cannot trust.
Keeping Old Dumps Without Filling the Disk
Retention is the part people forget until the disk fills up. A daily dump for a small WordPress site might be a few megabytes compressed, which sounds harmless, but a year of them adds up, and a busy WooCommerce database grows faster than you expect. Fourteen days of daily dumps plus four weekly dumps is a solid default for a small VPS. It covers the two cases that matter most: restoring yesterday’s data after a bad update, and restoring last week’s data after a problem that took days to notice.
Let the script delete old dumps itself with find, and make the retention window explicit in a comment at the top of the script. If you want longer history, move old dumps off the VPS instead of keeping them locally. An automatic mysql database backup is only half a strategy until a copy exists somewhere else. Copy the dumps to a second server or an object storage bucket as part of the same script, and confirm the copy lands before the local cleanup deletes anything. The 3-2-1 backup rule for servers explains why that second location matters more than any retention setting.
Testing a MySQL Restore Before You Need One
A dump file you have never restored is a guess. Test it on a scratch database, never on the live one. Create an empty database named restore_test, feed the dump into it with mysql restore_test < backup.sql, and check that the tables and row counts look right. For a compressed dump, decompress it first or pipe gunzip into mysql. The whole test takes a few minutes for a small database, and it proves two things at once: the dump is valid, and you remember the restore command.
Also test the case where the database is gone entirely. A restore that rebuilds the database from nothing, including creating the database and the user, is the one you will actually need after a serious failure. Write those steps down in a short runbook and keep it outside the VPS. When a database is down and the site is offline, you do not want to be reconstructing commands from memory.
Common mysqldump Mistakes
The most common mistake is dumping without –single-transaction on a busy InnoDB database and getting an inconsistent backup. The second is backing up to the same disk and calling it done, with no offsite copy. The third is a cron job that fails silently because its output goes nowhere. The fourth is storing dumps in a web-accessible folder. The fifth is using the root password in the crontab or in a world-readable script. Each of these is easy to avoid once and expensive to discover later.
Another quiet mistake is dumping the database but forgetting everything around it: uploaded files, themes, plugins, and configuration. A mysql backup script linux owners run every night covers the database only. The files need their own backup, and the two should be scheduled close together so a restore brings back a matching pair. The rsync backup tutorial for a second VPS covers the file side, and the comparison of backup tools for Docker volumes helps if your database runs in a container instead of directly on the host.
Set it up today: create the backup user, write the script, test it by hand, schedule it in cron, and restore it once into a scratch database. That is an afternoon of work for a backup that runs itself every night after. A database backup you never have to think about is the whole point, and mysqldump plus cron gets you there with tools already on the server.