Code in this video_How to Automatic Backup MySQL/MariaDB with Crontab in Linux

Code in this video_How to Automatic Backup MySQL/MariaDB with Crontab in Linux

📌 Replace the highlighted information with your own

#!/bin/bash

# MySQL database credentials
DB_USER="your_username"
DB_PASS="your_password"
DB_NAME="your_database"

# Backup directory
BACKUP_DIR="/path_to_backup_directory"

# Date format for backup file
DATE=$(date +"%Y%m%d_%H%M%S")

# Backup filename
BACKUP_FILE="$BACKUP_DIR/$DB_NAME-$DATE.sql.gz"

# Dump the MySQL database
mysqldump -u $DB_USER -p$DB_PASS $DB_NAME | gzip > $BACKUP_FILE

# Check if backup was successful
if [ $? -eq 0 ]; then
echo "Backup completed successfully: $BACKUP_FILE"
else
echo "Error: Backup failed!"
fi

### Crontab

0 23 * * * /path_to_backup_directory/backup_script.sh

Leave a Reply

Your email address will not be published. Required fields are marked *