forked from dale42/wp-site-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwp-site-sync.sh
More file actions
196 lines (165 loc) · 4.08 KB
/
wp-site-sync.sh
File metadata and controls
196 lines (165 loc) · 4.08 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
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
#!/usr/bin/env bash
# wp-site-sync.sh
#
# This shell script synchronizes a local WordPress dev to the its parent.
# The synchronization includes the file system. It works best is ssh key
# is setup with the parent host.
#
# Script Version
VERSION=0.1.2
# Required Configuration parameters
config_params=('name' 'remote_host' 'remote_port' 'remote_wp_dir' 'remote_backup_dir' 'remote_url' 'remote_type' 'local_wp_dir' 'local_backup_dir' 'local_url' 'local_type' 'date_stamp' 'backup_file_id')
# Constants
sites_directory=~/.wp-site-sync
#
# print_help
#
# Print a command summary
#
function print_help {
echo "The following options are supported:"
echo " list - Listed configured sites"
echo " @site - Sync the specified site"
#todo: Create site template
echo ""
}
#
# test_configuration_file
#
# Load the configuration file and verify it contains all of
# the required parameters.
#
function test_configuration_file {
filename=$sites_directory/$1
# file exists and is readable
if [ ! -r $filename ]
then
echo " ERROR: $filename does not exist or is not readable"
return 2
fi
# file has all of the required parameters defined
status=0
source $filename
for param in ${config_params[*]}
do
# Test if the parameter is set
if [ -z ${!param+x} ]
then
echo " ERROR: Value is not set for '$param'"
status=2
break
fi
done
return $status
}
#
# do_sync
#
# Syncronize the remote and local site using data from
# the configuration file.
#
function do_sync {
echo " Using sync file $1"
filename=$sites_directory/$1
source $filename
if [ "$backup_file_id" = "" ]
then
backup_file="$name-$remote_type-$date_stamp.sql.gz"
else
backup_file="$name-$backup_file_id-$remote_type-$date_stamp.sql.gz"
fi
echo " Site Sync: $name"
echo " - Backing up WordPress database on $remote_host"
ssh -p $remote_port $remote_host "cd $remote_wp_dir; wp db export - | gzip > $remote_backup_dir/$backup_file"
if [ $? -ne 0 ]; then
echo " ERROR: Could not backup remote database"
return 1
fi
echo " - Copying backup file to local directory $local_backup_dir"
scp -P $remote_port $remote_host:$remote_backup_dir/$backup_file $local_backup_dir/.
if [ $? -ne 0 ]; then
echo " ERROR: Could not backup remote backup to local"
return 1
fi
echo " - Deleting backup file $backup_file on $remote_host"
ssh -p $remote_port $remote_host "\rm -v $remote_backup_dir/$backup_file"
if [ $? -ne 0 ]; then
echo " ERROR: Could not delete remote database backup"
return 1
fi
echo " - Creating a safety backup of $local_type"
cd $local_wp_dir
wp db export - | gzip > $local_backup_dir/$name-$local_type-$date_stamp.sql.gz
echo " - Dropping all tables from local database"
cd $local_wp_dir
wp db reset --yes
echo " - Importing the database to local instance"
cd $local_wp_dir
gunzip -c $local_backup_dir/$backup_file | wp db import -
echo " - Fixing URLs"
wp search-replace $remote_url $local_url
echo " - rsyncing files"
RSYNC_COMMAND="rsync --progress -vrae 'ssh -p $remote_port' $remote_host:$remote_wp_dir/wp-content/uploads/ $local_wp_dir/wp-content/uploads"
eval $RSYNC_COMMAND
if [ $? -ne 0 ]; then
echo " ERROR: Could not synchronize local file system"
return 1
fi
}
#
#
# Main
#
#
echo ""
echo "wp-site-sync v$VERSION"
#
# Verify configuration
#
if [ ! -d $sites_directory ]
then
echo " ERROR: Configuration directory $sites_directory not found"
echo ""
exit 1
fi
#todo: Verify wp-cli installed
#
# Command Loop
#
if [ "$1" == "" ]
then
#
# Print help
#
print_help
elif [ "${1:0:1}" == "@" ]
then
#
# Execute Sync
#
config_file=${1:1}
echo " ..Verifying $config_file"
test_configuration_file $config_file
if [ $? -ne 0 ]; then
echo ""
exit 2
fi
echo " ..Syncing sites"
do_sync $config_file
elif [ $1 == 'list' ]
then
#
# List Sync files
#
echo "Available sync configurations:"
ls -1 $sites_directory
echo ""
else
#
# Invalid command
#
echo ""
echo "ERROR: Invalid command"
echo ""
print_help
fi