-
Notifications
You must be signed in to change notification settings - Fork 1
/
backup.sh
executable file
·187 lines (165 loc) · 6.81 KB
/
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/bin/bash
#==================================================================================================
#
# FILE: backup.sh
#
# USAGE: backup.sh [-c|--config=<path_to_config.json>] [project_name]
#
# DESCRIPTION: Backs up all projects in the config file.
# Optional parameter `project_name` used to backup a single project.
#
#==================================================================================================
# Load config file + helper methods before proceeding
SCRIPT_BASE=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)
. $SCRIPT_BASE/get-arguments.sh
. $SCRIPT_BASE/config-helper.sh
#=== FUNCTION ===================================================================================
# NAME: backup_files
# DESCRIPTION: Loops over the list of files (from config file) and backs them up
# PARAMETER 1: Project regex to filter project settings from config file
# PARAMETER 2: Backup path/destination
#==================================================================================================
function backup_files {
local project_regex=$1
local backup_path=$2
local count=$( get_count $( get_key_regex $project_regex "files" ) )
for ((i=0; i<$count; ++i ))
do
local db_name=$( get_values $( get_key_regex $project_regex "files" "$i" "name" ) )
local path=$( get_values $( get_key_regex $project_regex "files" "$i" "path" ) )
tar -cvf $backup_path/$db_name.tar -C $(dirname $path) $(basename $path)
done
}
#=== FUNCTION ===================================================================================
# NAME: backup_databases
# DESCRIPTION: Loops over the list of databases (from config file) and backs them up
# PARAMETER 1: Project regex to filter project settings from config file
# PARAMETER 2: Backup path/destination
#==================================================================================================
function backup_databases {
local project_regex=$1
local backup_path=$2
local count=$( get_count $( get_key_regex $project_regex "db" ) )
for ((i=0; i<$count; ++i ))
do
local db_name=$( get_values $( get_key_regex $project_regex "db" $i "name" ) )
local db_args=$( get_values $( get_key_regex $project_regex "db" $i "db_args" ) )
local db_user=$( get_values $( get_key_regex $project_regex "db" $i "user" ) )
local db_password=$( get_values $( get_key_regex $project_regex "db" $i "password" ) )
if [ $( get_values $( get_key_regex $project_regex "db" $i "type" ) ) == "mysql" ]
then
echo "Dumping MySQL db: $db_name"
if [ ! -z $db_user ] && [ ! -z $db_password ]
then
mysqldump --user=$db_user --password=$db_password --opt $db_name $db_args > $backup_path/$db_name.sql
elif [ ! -z $db_user ]
then
mysqldump --user=$db_user --opt $db_name $db_args > $backup_path/$db_name.sql
else
mysqldump --opt $db_name $db_args > $backup_path/$db_name.sql
fi
elif [ $( get_values $( get_key_regex $project_regex "db" $i "type" ) ) == "postgresql" ]
then
echo "Dumping PostgreSQL db: $db_name"
if [ ! -z $db_user ]
then
pg_dump -c -U $db_user $db_args $db_name -f $backup_path/$db_name.sql
else
pg_dump -c $db_args $db_name -f $backup_path/$db_name.sql
fi
fi
done
}
#=== FUNCTION ===================================================================================
# NAME: backup_project
# DESCRIPTION: Backs up a single project.
# Executes `backup_files` and `backup_db` for the given project.
# PARAMETER 1: Project name
# PARAMETER 2: Backup root folder
#==================================================================================================
function backup_project {
local project_name=$1
local backup_root=$2
local backup_name=$project_name-`date +%Y%m%d%H%M%S`
local backup_path=$backup_root/$backup_name
local project_id=$( get_project_id $project_name )
mkdir -p $backup_path
echo "`backup_files "$( get_key_regex projects $project_id )" "$backup_path"`"
echo "`backup_databases "$( get_key_regex projects $project_id )" "$backup_path"`"
tar -zcvf $backup_path.tar.gz -C $backup_root $backup_name
rm -r $backup_path
# Backup to remote host
local remote_host=$( get_values $( get_key_regex remote host ) )
if [ ! -z $remote_host ]
then
scp $backup_path.tar.gz $remote_host:$( get_values $( get_key_regex remote path ) )
fi
}
#=== FUNCTION ===================================================================================
# NAME: backup_projects
# DESCRIPTION: Backs up all project in config file.
# Executes `backup_project` for each project in config file.
# PARAMETER 1: Backup root folder
#==================================================================================================
function backup_projects {
local count=$( get_count $( get_key_regex "projects" ) )
for ((i=0; i<$count; ++i ))
do
local project_name=$( get_values $( get_key_regex "projects" "$i" "name" ) )
echo "`backup_project "$project_name" "$@"`"
done
}
#=== FUNCTION ===================================================================================
# NAME: main
# DESCRIPTION: Verifies config + arguments and then initiates backup process
# PARAMETER 1: Project name (optional)
#==================================================================================================
function main {
# Get `backup_root` from config file
local backup_root=`echo $( get_values $( get_key_regex "root" ) ) | sed -e "s/\/$//g"`
# Backup root should exist in config file
if [ -z $backup_root ]
then
echo "[ERROR] Backup root not in config file. Backup aborted."
exit 1
fi
# Backup root should not be a file.
if [ -f $backup_root ]
then
echo "[ERROR] Backup root is a file. Backup aborted."
exit 1
fi
# $1 should be a valid project name in the config file
if [ ! -z $1 ] && [ -z $( get_project_id $1 ) ]
then
echo "[ERROR] Project name not found in config file. Backup aborted."
exit 1
fi
# Capture project name
local backup_project_name=$1
# Project should exist in config file
if [ ! -z $backup_project_name ] && [ -z $( get_project_id $backup_project_name ) ]
then
echo "[ERROR] Project not found in config file. Backup aborted."
exit 1
fi
# Create backup root folder if it does not exist.
if [ ! -d $backup_root ]
then
mkdir -p $backup_root
fi
# Execute backup command
if [ ! -z $backup_project_name ]
then
# Execute backup_project function
echo "`backup_project "$backup_project_name" "$backup_root"`"
else
# Execute backup_projects function
echo "`backup_projects "$backup_root"`"
fi
}
# Run main if not being executed as a part of the tests.
if [ -z $BACKUP_TESTING ]
then
main $@ >&1
fi