Knowledgebase

Backup MySQL databases daily on Debian

You own a Debian OS at your VPS / VDS / dedicated server and want to automatically daily secure all of your MySQL- or MariaDB databases? Then you've come to the right place!

In this article, we'll show you how to automatically export all your MySQL databases on a Debian operating system.

First, we need to make sure we have the latest updates installed on our server. You can achieve this with a simple command:

apt update && apt upgrade -y

 

If you haven't installed the Nano text editor yet, you can do so with the following command:

apt install nano -y

Now we create a Bash script that takes on the task of exporting and deleting the oldest backups. For our example, we call the script 'mysql_export_all.sh' and save it in the directory '/opt/mysqlbackups':

 

mkdir /opt/mysqlbackups/
nano /opt/mysqlbackups/mysql_export_all.sh

 

In this script we could write the following:

 

#!/bin/bash

USER='root'
PASSWORD='yourpassword'
DATE=$(date +%Y-%m-%d-%H-%M)
BACKUP_DIR='/opt/mysqlbackups'

mkdir -p $BACKUP_DIR

mysqldump -u$USER -p$PASSWORD --all-databases > $BACKUP_DIR/alldbs_$DATE.sql

find $BACKUP_DIR -not -name "$(basename "$0")" -mtime +7 -exec rm {} \;

Remember to replace 'root' and 'yourpassword' with your MySQL username and password.

 

Now we make the script executable:

 

chmod +x /opt/mysqlbackups/mysql_export_all.sh

 

Now we need to add this script to our cron job. Use the following command to open the cron job editor:

 

export VISUAL=nano; crontab -e

 

For a daily backup at 5 AM, we could add the following:

 

0 5 * * * /opt/mysqlbackups/mysql_export_all.sh

 

With this setup, all your MySQL databases will now be automatically exported every day at 5 AM and backups older than 7 days will automatically be deleted.

 


 

Do you have a vServer / root server and would like to have more performance? Then a look at our range of root servers couldn't hurt!

With the discount code "KernelHost-Tutorials" you also receive a 10% discount (permanent) on your tariff!

More details:

Hardware: https://www.kernelhost.com/en/hardware

Datacenter: https://www.kernelhost.com/en/datacenter

DDoS-Protection: https://www.kernelhost.com/en/ddos-protection

PrePaid: https://www.kernelhost.com/en/prepaid

Didn't the instructions help you? You can contact us here via ticket! We're here to help.

 

© KernelHost.com - Re-posting these instructions on your website is not permitted.

  • MySQL, Backup, MySQL Backup, MariaDB, MariaDB Backup
  • 17 Users Found This Useful

Was this answer helpful?

Related Articles

[Debian 8 (Jessie)] Update über apt-get update schlägt fehl

Sie wollen Ihren Server mit Debian 8 updaten, und das schlägt fehl? Dann sind Sie hier genau...

Mail-Server (modoboa) auf Debian 11 & Debian 10 installieren

Sie möchten gerne einen eigenen Mailserver auf Ihren Debian 10 / Debian 11 Server installieren?...

MySQL | Apache2, PHP8, MySQL und PHPMyAdmin auf Debian 11 & Debian 10 installieren

Sie möchten gerne Apache2, PHP8, MySQL (bzw. MariaDB) und PHPMyAdmin (LaMp) auf Ihrem Debian 10 /...

NodeJS-10 / NodeJS-12 auf Debian 11 & Debian 10 installieren

Sie möchten gerne NodeJS und npm auf Ihren Debian-10 / Debian 11 Server installieren? Dann sind...

Java-8 / Java-11 auf Debian 11 & Debian 10 installieren

Sie möchten gerne Java-8 oder Java-11 auf Ihrem Debian 11 / Debian 12 Betriebssystem...