backup a database every day using Bash

This script first sets the name of the database, the username and password used to connect to the database, the directory where the backup file will be saved, and the filename of the backup file using the current date.
It then use the mysqldump command to dump the database to a SQL file and also compress it using gzip command.

#!/bin/bash

# Set the name of the database
database=mydb

# Set the username and password for the database
user=myuser
password=mypassword

# Set the location to save the backup file
backup_dir=/path/to/backup/directory

# Set the filename for the backup file
date=$(date +"%Y-%m-%d")
backup_file="$backup_dir/$database-$date.sql"

# Dump the database to a SQL file
mysqldump -u $user -p$password $database > $backup_file

# Compress the SQL file
gzip $backup_file

This script first sets the name of the database, the username and password used to connect to the database, the directory where the backup file will be saved, and the filename of the backup file using the current date.
It then use the mysqldump command to dump the database to a SQL file and also compress it using gzip command.

You can then schedule this script to run every day using crontab, for example, you can add the following entry to your crontab to run the script every day at 2 am:

——-
0 2 * * * /path/to/backup-database.sh
——-

It’s worth noting that this script is just one example of how to backup a database, and there are other options available such as using different database management system or using other backup utilities like pg_dump for postgresql or mongodump for MongoDB.

Total
0
Shares
კომენტარის დატოვება

თქვენი ელფოსტის მისამართი გამოქვეყნებული არ იყო. აუცილებელი ველები მონიშნულია *

Previous Post

shell script that finds log files 

Next Post

DevOps Project (using Docker, Jenkins and AWS)

Related Posts