-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions
210 lines (196 loc) · 5.64 KB
/
functions
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/bin/bash
# FUNCTIONS FOR SYNC
##the main script
syncSystems() {
#confirmation warning message
startingMessage
text_red
read -r -p "Are you sure you want to overwrite ${!DB_DEST} ? [y/N] ..." response
response=${response,,} # tolower
if [[ "$response" =~ ^(yes|y)$ ]]
then
#mode 1 = all
#mode 2 = files only
#mode 3 = db only
#warning ok
text_reset
#debug messages
printVariables
#backup destination database
if [ $MODE -ne 2 ] ; then backupDestinationDatabase; fi
#backup source database
if [ $MODE -ne 2 ] ; then backupSourceDatabase; fi
#import source db into destination db
if [ $MODE -ne 2 ] ; then importSourceDatabase; fi
#loop through all the directories set in include file
if [ $MODE -ne 3 ] ; then syncAllItems; fi
#done message
finishedMessage
else
#warning message - cancelled
echo ABORTED!
fi
}
##rsync all locations in includes.list
syncAllItems () {
text_green
echo "Syncing files changed in ${SOURCE} to ${DESTINATION}"
text_yellow
echo "${i}..."
text_reset
if [ "${!SSH_SRC_NAME}" != "" ] ; then SSH_SRC=${!SSH_SRC_NAME}\:; fi
if [ "${!SSH_DEST_NAME}" != "" ] ; then SSH_DEST=${!SSH_DEST_NAME}\:; fi
if [ "$DEBUG" = true ] ; then
debugNotice
echo "rsync -azi --info=progress2 --recursive --exclude-from=${EXCLUDES} --files-from=${INCLUDES} ${SSH_SRC}${!BASE_SRC}/ ${SSH_DEST}${!BASE_DEST}/"
text_reset
else
#rsync -azi --info=progress2 --recursive --exclude-from=excludes.list --files-from=includes.list /vagrant/ irata-test:/var/www/clients/client4/web10/
rsync -azi --info=progress2 --recursive --exclude-from=${EXCLUDES} --files-from=${INCLUDES} ${SSH_SRC}${!BASE_SRC}/ ${SSH_DEST}${!BASE_DEST}/
echo Files transfered
fi
}
createVariables () {
## these will change for each sync type
DB_SRC_CONF=${SOURCE}_MYSQL_CONF
DB_SRC=${SOURCE}_DB_NAME
DB_DEST_CONF=${DESTINATION}_MYSQL_CONF
DB_DEST=${DESTINATION}_DB_NAME
#flip rsync from upload to download
if [ $SOURCE = "DEV" ] ; then
SSH_SRC_NAME=""
SSH_DEST_NAME=${DESTINATION}_SSH
else
SSH_SRC_NAME=${SOURCE}_SSH
SSH_DEST_NAME=""
fi
#base paths
BASE_DEST=${DESTINATION}_PATH
BASE_SRC=${SOURCE}_PATH
}
backupDestinationDatabase () {
text_green
echo "Backing up ${DESTINATION} database..."
text_reset
if [ "$DEBUG" = true ] ; then
debugNotice
echo "mysqldump --defaults-file=${!DB_DEST_CONF} ${!DB_DEST} | gzip | pv --size $SIZE_COMPRESSED > $DB_DEST_BACKUP"
text_reset
else
mysqldump --defaults-file=${!DB_DEST_CONF} ${!DB_DEST} | gzip | pv --size $SIZE_COMPRESSED > $DB_DEST_BACKUP
echo Backup done
fi
}
backupSourceDatabase () {
#separator
text_green
echo "Backing up ${SOURCE} database..."
text_reset
if [ "$DEBUG" = true ] ; then
debugNotice
echo "mysqldump --defaults-file=${!DB_SRC_CONF} ${!DB_SRC} | gzip | pv --size $SIZE_COMPRESSED > $DB_SRC_BACKUP"
text_reset
else
mysqldump --defaults-file=${!DB_SRC_CONF} ${!DB_SRC} | gzip | pv --size $SIZE_COMPRESSED > $DB_SRC_BACKUP
echo Backup done
fi
}
importSourceDatabase () {
#separator
text_green
echo "Importing ${SOURCE} mysqldump..."
text_reset
if [ "$DEBUG" = true ] ; then
debugNotice
echo "zcat $DB_SRC_BACKUP | pv --size $SIZE | mysql --defaults-file=${!DB_DEST_CONF} ${!DB_DEST}"
text_reset
else
zcat $DB_SRC_BACKUP | pv --size $SIZE | mysql --defaults-file=${!DB_DEST_CONF} ${!DB_DEST}
echo Import done
fi
}
finishedMessage () {
text_yellow
echo ""
echo "DONE :)"
echo ""
text_reset
echo "${DESTINATION} synced with ${SOURCE}"
echo ""
if [ "$DEBUG" = true ] ; then
separator
echo "END DEBUG"
separator
fi
}
startingMessage () {
if [ "$DEBUG" = true ] ; then
separator
echo "START DEBUG"
separator
fi
}
printVariables () {
if [ "$DEBUG" = true ] ; then
echo "VARIABLES:"
echo "-------------"
echo "DIR = $DIR"
echo "MODE = $MODE"
echo "SOURCE = $SOURCE"
echo "DESTINATION = $DESTINATION"
echo ""
echo "DB_SRC_CONF = ${!DB_SRC_CONF}"
echo "DB_SRC = ${!DB_SRC}"
echo "DB_DEST_CONF = ${!DB_DEST_CONF}"
echo "DB_DEST = ${!DB_DEST}"
echo "SSH_SRC_NAME = ${!SSH_SRC_NAME}"
echo "SSH_DEST_NAME = ${!SSH_DEST_NAME}"
echo "DB_SRC_BACKUP = $DB_SRC_BACKUP"
echo "DB_DEST_BACKUP = $DB_DEST_BACKUP"
echo "BASE_DEST = ${!BASE_DEST}"
echo "BASE_SRC = ${!BASE_SRC}"
echo ""
echo "SIZE = $SIZE"
echo "SIZE_COMPRESSED = $SIZE_COMPRESSED"
echo ""
fi
}
debugNotice () {
echo "-- IN DEBUG MODE -- the following will be executed:"
text_yellow
}
separator () { printf $HR; }
text_reset () { printf "${ColourOff}"; }
text_red () { printf "${Red}"; }
text_green () { printf "${Green}"; }
text_yellow () {
printf "${Yellow}"
[ "$#" -gt 0 ] && echo "$@"; text_reset
}
text_bold () {
printf "${BWhite}"
[ "$#" -gt 0 ] && echo "$@"; text_reset
}
print_usage () {
echo ""
text_bold "NAME"
echo "$TAB DEV-to-STAGING - sync staging server with dev using rsync"
echo "$TAB STAGING-to-DEV - sync dev server with staging using rsync"
echo "$TAB DEV-to-PRODUCTION - sync live server with dev using rsync"
text_bold "SYNOPSIS"
echo "$TAB At last... a sync script! v1"
text_bold "DESCRIPTION"
echo "$TAB Sync $SOURCE to $DESTINATION"
echo "$TAB read README.md for config file settings"
echo "$TAB -d, --db-only only sync databases"
echo "$TAB -f, --files-only only sync files"
echo "$TAB -h, -?, --help show this help"
echo ""
text_bold "EXAMPLE USAGE:"
echo "$TAB ./DEV-to-STAGING -d sync db from local to staging version"
echo "$TAB ./STAGING-to-DEV -f sync files from staging to local"
echo ""
echo "for more help see: "
text_bold "https://github.com/emrysr/sync"
echo "emrys@matrix10.net 2017-05-15";
}