-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackup-test.sh
159 lines (117 loc) · 5.02 KB
/
backup-test.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
#!/usr/bin/env roundup
describe "backup: Backs up all projects in the config file."
SCRIPT_BASE=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)
TEST_CONFIG_FILE=$SCRIPT_BASE/tests/config/backup-test.json
DIFF_EXCLUDE_PATTERNS_FILE=$SCRIPT_BASE/tests/config/exclude.txt
. $SCRIPT_BASE/config-helper.sh
BACKUP_TESTING=1
. $SCRIPT_BASE/backup.sh
TEMP_ROOT=$SCRIPT_BASE/tests/temp
MYSQL_TEST_DATA=$SCRIPT_BASE/tests/mysql.sql
POSTGRESQL_TEST_DATA=$SCRIPT_BASE/tests/postgresql.sql
before() {
# Set the Backup Config File and read Backup Root
BACKUP_CONFIG_FILE=$TEST_CONFIG_FILE
BACKUP_ROOT=$SCRIPT_BASE/`get_values $(get_key_regex root)`
# Create + clean working folders
[ -d $BACKUP_ROOT ] && rm -r $BACKUP_ROOT
[ -d $BACKUP_ROOT ] || mkdir -p $BACKUP_ROOT
[ -d $TEMP_ROOT ] && rm -r $TEMP_ROOT
[ -d $TEMP_ROOT ] || mkdir -p $TEMP_ROOT
}
after() {
# Delete working folders
[ -d $BACKUP_ROOT ] && rm -r $BACKUP_ROOT
[ -d $TEMP_ROOT ] && rm -r $TEMP_ROOT
}
it_backs_up_files() {
# Backup files
backup_files "$(get_key_regex projects 0)" "$BACKUP_ROOT"
# Create tar of the files in the temporary directory
local path=$SCRIPT_BASE/$(get_values $(get_key_regex projects 0 files 0 path))
tar -cvf $TEMP_ROOT/f.tar -C $(dirname $path) $(basename $path)
local path=$SCRIPT_BASE/$(get_values $(get_key_regex projects 0 files 1 path))
tar -cvf $TEMP_ROOT/d.tar -C $(dirname $path) $(basename $path)
# Compare backup and temporary directories
test -z `diff -r --exclude-from='$DIFF_EXCLUDE_PATTERNS_FILE' $BACKUP_ROOT $TEMP_ROOT`
}
it_backs_up_mysql_db() {
local project_id=$( get_project_id mysql_project )
if [ ! -z $project_id ]
then
# Get the database credentials
local db_name=$( get_values $( get_key_regex projects $project_id db 0 name ) )
local db_user=$( get_values $( get_key_regex projects $project_id db 0 user ) )
local db_password=$( get_values $( get_key_regex projects $project_id db 0 password ) )
# Load database dump, and export a dump in the temporary directory
if [ ! -z $db_user ] && [ ! -z $db_password ]
then
mysql --user=$db_user --password=$db_password $db_name < $MYSQL_TEST_DATA
mysqldump --user=$db_user --password=$db_password --opt $db_name > $TEMP_ROOT/$db_name.sql
elif [ ! -z $db_user ]
then
mysql --user=$db_user $db_name < $MYSQL_TEST_DATA
mysqldump --user=$db_user --opt $db_name > $TEMP_ROOT/$db_name.sql
else
mysql $db_name < $MYSQL_TEST_DATA
mysqldump --opt $db_name > $TEMP_ROOT/$db_name.sql
fi
# Backup databases
backup_databases "$( get_key_regex projects $project_id )" "$BACKUP_ROOT"
# Compare database dumps in the backup and temporary directories
test -z `diff -r --exclude-from='$DIFF_EXCLUDE_PATTERNS_FILE' --ignore-matching-lines='^--' $BACKUP_ROOT $TEMP_ROOT`
fi
}
it_backs_up_postgresql_db() {
local project_id=$( get_project_id postgresql_project )
if [ ! -z $project_id ]
then
# Get the database credentials
local db_name=$( get_values $( get_key_regex projects $project_id db 0 name ) )
local db_user=$( get_values $( get_key_regex projects $project_id db 0 user ) )
# Load database dump, and export a dump in the temporary directory
if [ ! -z $db_user ]
then
psql -U $db_user -d $db_name -f $POSTGRESQL_TEST_DATA
pg_dump -c -U $db_user $db_name -f $TEMP_ROOT/$db_name.sql
else
psql -d $db_name -f $POSTGRESQL_TEST_DATA
pg_dump -c $db_name -f $TEMP_ROOT/$db_name.sql
fi
# Backup databases
backup_databases "$( get_key_regex projects $project_id )" "$BACKUP_ROOT"
# Compare database dumps in the backup and temporary directories
test -z `diff -r --exclude-from='$DIFF_EXCLUDE_PATTERNS_FILE' --ignore-matching-lines='^--' $BACKUP_ROOT $TEMP_ROOT`
fi
}
it_backs_up_remote() {
# Load the Remote Backup Config File and read Backup Root
BACKUP_CONFIG_FILE=$SCRIPT_BASE/tests/config/backup-remote-test.json
BACKUP_ROOT=$SCRIPT_BASE/`get_values $(get_key_regex root)`
local remote_host=$( get_values $( get_key_regex remote host ) )
local project_name=$(get_project_name 0)
# Backup project (including remote)
backup_project $project_name $BACKUP_ROOT
# Retrieve backups from remote host
if [ ! -z $remote_host ]
then
# Retrieve backups from remote host
for backup_file in `ls $BACKUP_ROOT | grep ^$project_name\-[0-9]*\.tar\.gz$`
do
if [ -f $BACKUP_ROOT/$backup_file ]
then
scp $remote_host:$( get_values $( get_key_regex remote path ) )/$backup_file $TEMP_ROOT
fi
done
# Compare backup and temporary directories
test -z `diff -r $BACKUP_ROOT $TEMP_ROOT`
# Delete test backups from remote system
for backup_file in `ls $BACKUP_ROOT | grep ^$project_name\-[0-9]*\.tar\.gz$`
do
if [ -f $BACKUP_ROOT/$backup_file ]
then
ssh $remote_host "rm $( get_values $( get_key_regex remote path ) )/$backup_file"
fi
done
fi
}