Jump to Navigation

021 - MySQL Backup Script Sample1

This is a sample backup script to keep the mysql dump for 3 days.

Example:

#!/bin/bash

# Set parameters for MySQL
DBNAME=database_name
#HOST=host_name
USER=user_name
PASSWORD=password

# Set parameters for the backup path
ROOT_PATH=/var/www/vhosts/your domain/data
BKUP_PATH=/var/www/vhosts/your domain/backups

# Create file name
BKUP_DATE=$(date +%d-%m-%y)
BKUP_DEL=$(date --date '3 days ago' +%d-%m-%y)
BKUP_FILENAME1=$BKUP_PATH/mysql-$DBNAME-$BKUP_DATE.'sql.gz'
BKUP_FILENAME2=$BKUP_PATH/DIR-$DBNAME-$BKUP_DATE.'tar.gz'
BKUP_FILENAME3=$BKUP_PATH/mysql-$DBNAME-$BKUP_DEL.'sql.gz'
BKUP_FILENAME4=$BKUP_PATH/DIR-$DBNAME-$BKUP_DEL.'tar.gz'

# Delete files
rm -rf $BKUP_FILENAME1
rm -rf $BKUP_FILENAME2
rm -rf $BKUP_FILENAME3
rm -rf $BKUP_FILENAME4

# Backup MySQL
#mysqldump -u $USER -h $HOST -p$PASSWORD $DBNAME | gzip -9 > $BKUP_FILENAME1
mysqldump -u $USER -p$PASSWORD $DBNAME | gzip -9 > $BKUP_FILENAME1

# Backup root Directory
tar cvf - $ROOT_PATH | gzip -9 > $BKUP_FILENAME2

or

tar zcvf $BKUP_FILENAME2 $ROOT_PATH (this is fester than gzip).

 

 

Linux:
MySQL:


Main menu 2

Story | by Dr. Radut