-
Notifications
You must be signed in to change notification settings - Fork 154
/
mysql_backup.sh
72 lines (53 loc) · 2.46 KB
/
mysql_backup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/bin/bash
# Script Name : mysql_backup.sh
# Author : Craig Richards
# Created : 08-March-2012
# Last Modified :
# Version : 1.0
# Modifications :
# Description : Backup MySQL database, either the full database or just a single database
#################################
# Start of procedures/functions #
#################################
funct_check_params() # Function Name
{ # Start of the function
if [ ${NARG} -lt 1 ]; then # If the number of arguments is not one, then output a message
echo "Usage: $0 argument"
echo "full = Full MySQL Backup"
echo "db dbname = Single Database backup"
exit 1 # Quit the program
elif
# If the argument passed is -h or --h then display the following message in the echo statement
[[ ${choice} = "-h" ]] || [[ ${choice} = "--h" ]]; then
echo "Usage: $0 argument"
echo "full = Full MySQL Backup"
echo "db dbname = Single Database backup"
exit 1 # Quit the program
fi # End of the if statement
} # End of the function
funct_backup() # Function Name
{ # Start of the function
if [ ${choice} = "full" ]; then # If the choice is full, then..
mkdir -p ${BACKUP_DIR} # Make the backup directory
# Run the command to backup ALL the databases in MySQL, timestamp the file to the dated folder
mysqldump --all-databases | gzip > ${BACKUP_DIR}/full_mysql_backup-`date +%H:%M`.gz
elif # Or
[[ ${choice} = "db" ]]; then # If the choice is db, then..
mkdir -p ${BACKUP_DIR} # Make the backup directory
# Backup just the given database, timestamp the file to the dated folder
mysqldump ${dbbkup} | gzip > ${BACKUP_DIR}/${dbbkup}_mysql_backup-`date +%H:%M`.gz
fi
} # End of the function
################
# Main Program #
################
# Variable Settings
DATE=`date +"%d-%B-%Y"` ; export DATE # Set the DATE variable, format it as 20-February-2012
choice=$1 # Set the variable choice as the first argument passed
dbbkup=$2 # Set the variable dbbkup as the database to backup
NARG=$# # Set the variable NARG to a number of arguments on the command line
{ # Start of the main program
funct_check_params # Call the function funct_check_params
funct_backup # Call the function funct_backup
} # End of the main program
## End of Script