-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbackup.sh
More file actions
65 lines (53 loc) · 2.06 KB
/
backup.sh
File metadata and controls
65 lines (53 loc) · 2.06 KB
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
#!/bin/bash
################################################################################
# Date : 2013/12/12
# Author : Vignesh Gurusamy
# Program : MySQL backup script
################################################################################
# Declare the variable
# Please add a new vaiable for additional database
# Associative array will work only after bash version 4
declare -A db1 db2
# MySQL Variable
# MySQL user should have permission to access the database
# Create an array like this for adding a new database
# Example :
# db1=(["hostname"]="localhost" ["username"]="root" ["password"]="" ["dbname"]="test" ["siteurl"]="xyz.com")
db1=(["hostname"]="localhost" ["username"]="root" ["password"]="" ["dbname"]="test" ["siteurl"]="xyz.com")
db2=(["hostname"]="localhost" ["username"]="root" ["password"]="" ["dbname"]="test1" ["siteurl"]="xyz.com")
# Please insert the database variable in this array
# Example :
# db_set=(db1 db2)
db_set=(db1 db2)
# Backup & Log Variable
db_bk_path="/opt/backup"
log_file="backuplog.log"
# Enable backup removal (true or false) and
bk_remove="true"
# Days after sql dump will be deleted from the system
bk_remove_days=10
# Create the folder if the backup location is not available
if [ ! -d "$db_bk_path/$(date +%Y-%m-%d)" ]; then
mkdir -p "$db_bk_path/$(date +%Y-%m-%d)"
fi
# Create the log file if it is not available
if [ ! -f "$db_bk_path/$log_file" ]; then
echo "" > "$db_bk_path/$log_file"
fi
# Remove the old backup dumps
if [ $bk_remove == "true" ]; then
find $db_bk_path -maxdepth 1 -type d -mtime +$bk_remove_days -exec rm -rf {} \;
fi
# Backup database on the system
for db in ${db_set[*]}
do
hostname="$db["hostname"]"
username="$db["username"]"
password="$db["password"]"
dbname="$db["dbname"]"
siteurl="$db["siteurl"]"
#MySQLdump cmd
mysqldump -u ${!username} -p${!password} -h ${!hostname} ${!dbname} | gzip > "$db_bk_path/$(date +%Y-%m-%d)/mysql-${!siteurl}-${!dbname}-$(date +%Y-%m-%d).sql.gz";
done
#completed status
echo "Backup completed on `date +%Y-%m-%d %H:%M:%S`" >> "$db_bk_path/$log_file"