171 views
asked in MariaDB by
Schedule a backup script for MariaDB on Ubuntu 24.04 LTS using cron

1 Answer

answered by
Here’s a step-by-step guide to schedule a backup script for MariaDB on Ubuntu 24.04 LTS using cron.

1. Install Required Tools

Ensure mariadb-client and cron are installed:

sudo apt update

sudo apt install mariadb-client cron -y

2. Create a Backup Directory

Create a directory to store backups:

sudo mkdir -p /var/backups/mariadb

sudo chown -R mysql:mysql /var/backups/mariadb

sudo chmod 555 -R /var/backups/mariadb

3. Create the Backup Script

Create a script to perform the backup:

sudo nano /usr/local/bin/mariadb_backup.sh

Add the following content:

#!/bin/bash

# Variables

BACKUP_DIR="/var/backups/mariadb"

DATE=$(date +'%Y-%m-%d_%H-%M-%S')

BACKUP_FILE="$BACKUP_DIR/mariadb21247_backup_$DATE.sql"

USER="root"

PASSWORD="lImfwsER@3034!"

# Backup command

mysqldump -u $USER -p$PASSWORD --all-databases > $BACKUP_FILE

# Compress the backup

gzip $BACKUP_FILE

# Remove backups older than 7 days

find $BACKUP_DIR -type f -name "*.sql.gz" -mtime +7 -exec rm {} \;

Replace your_password with your MariaDB root password.

Adjust -mtime +7 if you want to keep backups for more or fewer days.

Save and close the file.

4. Make the Script Executable

sudo chmod +x /usr/local/bin/mariadb_backup.sh

5. Schedule the Backup Script with Cron

Open the cron editor:

sudo crontab -e

Add the following line to schedule a daily backup at 6:00 AM:

0 6 * * * /usr/local/bin/mariadb_backup.sh >> /var/log/mariadb_backup.log 2>&1

0 6 * * *: Run daily at 6:00 AM.

>> /var/log/mariadb_backup.log 2>&1: Log output and errors.

Save and close the file.

6. Verify Cron Job

Check if the cron job is scheduled:

sudo crontab -l

Ensure your script is listed.

7. Test the Backup Script Manually

Run the script manually to verify it works:

sudo /usr/local/bin/mariadb_backup.sh

Check the backup directory:

ls -l /var/backups/mariadb

8. Verify Logs

Check the log file for any errors:

sudo cat /var/log/mariadb_backup.log

Your MariaDB backup script is now scheduled and will run daily at 6:00 AM.

Most popular tags

laravel postgresql laravel-10 replication ha postgresql mongodb laravel-11 mongodb database mongodb tutorial ubuntu 24.04 lts streaming-replication mysql database laravel postgresql backup laravel login register logout database mysql php laravel 11 - login with otp valid for 10 minutes. user and admin registration user and admin login multiauth technlogy asp.net asp.net c# mysql master slave replication centos linux laravel sql server schedule backup autobackup postgresql django python haproxy load balancer install self sign ssl laravel 11 gaurds zabbix 7 how to install graylog on ubuntu 24.04 lts | step-by-step asp.net core mvc .net mvc network upload c# ssl integration sql server on ubuntu 22.04 lts mssql server ms sql server sql server user access in postgres mysql password change cent os linux configure replica laravel 11 socialite login with google account google login kubernetes (k8s) install nginx load balancer install install and configure .net 8.0 in ubuntu 24.04 lts php in iis php with iis php tutorial chirags php tutorials chirags php tutorial chirags tutorial laravel 11 guards mongodb sharding metabase business analytics metabase postgresql 16 to postgresql 17 postgresql migration letsencrypt mongodb crud rocky linux laravel custom captcha laravel 11 captcha laravel captcha mongo dll php.ini debian 12 nginx apache nextcloud gitea in ubuntu git gitea npm error node js mysql ndb cluster mysql cluster ssl oracle login register logout in python debian windows shell batch file bat file time stamp date time shopping cart in laravel centos rhel swap memeory rhel 5.5
...