Automatic Daily Backup for MySQL/MariaDB using MySQLDump

To automatically backup the MySQL or MariaDB database daily using MySQLDump, you can follow the steps below:

Create a backup script file

Create a script file (e.g., backup.sh) to contain the backup commands. Open a text editor and add the following commands to the script file:

#!/bin/bash

# Replace the database connection information
DB_USER="username"
DB_PASSWORD="password"
DB_NAME="database_name"

# Path to the backup directory
BACKUP_DIR="/path/to/backup/directory"

# Create a backup file name with date format
BACKUP_FILE="$BACKUP_DIR/backup-$(date +%Y-%m-%d).sql"

# Use mysqldump command to backup the database
mysqldump -u$DB_USER -p$DB_PASSWORD $DB_NAME > $BACKUP_FILE

# Print a completion message when the backup is done
echo "Backup completed: $BACKUP_FILE"

Save the script file and ensure it has executable permissions. To do this, run the following command:

chmod +x backup.sh

 

Set up an automatic backup job

Use the cron scheduler to set up a daily automatic backup job. Open the cron schedule by running the command:

crontab -e

Add the following line to the cron schedule file to set up a daily backup job at 2 AM:

0 2 * * * /path/to/backup.sh

Save and close the cron schedule file.

The backup.sh script will then be executed daily at 2 AM, and it will backup the MySQL or MariaDB database to the backup-YYYY-MM-DD.sql file in the specified directory.

 

Note that in the script, you need to replace username, password, and database_name with the actual login information and database name. Similarly, change /path/to/backup/directory to the actual backup storage directory path on your system.