Ubuntu Linux Backup MySQL Server Shell Script

Ubuntu Linux Backup MySQL Server Shell Script

Here is a simple shell script that can be used to backup a MySQL server on Ubuntu Linux:

#!/bin/bash

# Set variables
TIMESTAMP=$(date +%Y-%m-%d)
BACKUP_DIR="/path/to/backup/directory"
MYSQL_USER="mysql_username"
MYSQL_PASSWORD="mysql_password"

# Create backup directory if it does not exist
if [ ! -d "$BACKUP_DIR" ]; then
  mkdir -p "$BACKUP_DIR"
fi

# Dump all databases
mysqldump --user=$MYSQL_USER --password=$MYSQL_PASSWORD --all-databases > "$BACKUP_DIR/mysql-$TIMESTAMP.sql"

# Compress the backup file
gzip "$BACKUP_DIR/mysql-$TIMESTAMP.sql"
So‮ru‬ce:www.lautturi.com

This script sets a few variables, such as the timestamp, backup directory, and MySQL user and password. It then creates the backup directory if it does not exist and uses the mysqldump command to create a backup of all databases. Finally, it uses the gzip command to compress the backup file.

To use this script, save it to a file (e.g. mysql-backup.sh) and make it executable by running the following command:

chmod +x mysql-backup.sh

You can then run the script by typing the following command:

./mysql-backup.sh

This will create a compressed MySQL backup file in the specified backup directory.

Keep in mind that this is a basic example of a MySQL backup script and may not meet the specific needs of your environment. You may want to customize the script to suit your requirements, such as by including additional options or commands, or by adding error handling or logging.

For more information on backing up MySQL on Ubuntu Linux, you can refer to the MySQL documentation or the Ubuntu documentation.

Created Time:2017-10-30 14:27:25  Author:lautturi