-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwp-dbsync
More file actions
executable file
·138 lines (109 loc) · 4.58 KB
/
wp-dbsync
File metadata and controls
executable file
·138 lines (109 loc) · 4.58 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
#!/bin/bash
# Dynamically find CONFIG_FILE
config_files=(Movefile.yaml movefile.yaml Movefile movefile movefile.yml Movefile.yaml)
matches=()
for file in "${config_files[@]}"; do
if [[ -f "$file" ]]; then
matches+=("$file")
fi
done
if [ ${#matches[@]} -eq 0 ]; then
echo "Error: No Movefile found (Movefile.yaml, movefile.yaml, Movefile, movefile, movefile.yml, Movefile.yaml)."
exit 1
elif [ ${#matches[@]} -gt 1 ]; then
echo "Error: Multiple Movefiles found: ${matches[@]}"
exit 1
fi
CONFIG_FILE="${matches[0]}"
# Check if yq is installed
if ! command -v yq &> /dev/null; then
echo "yq is not installed. Install it using 'brew install yq' or visit https://github.com/mikefarah/yq"
exit 1
fi
# Function to check if environment exists in the Movefile
check_environment() {
env_name=$1
if [[ "$env_name" == "local" ]]; then
echo "Error: 'local' is not a valid environment name."
exit 1
fi
if ! yq e ".${env_name}" $CONFIG_FILE &> /dev/null; then
echo "Error: Environment '$env_name' not found in $CONFIG_FILE."
exit 1
fi
}
# Function to pull database from remote to local
pull_db() {
remote_env=$1
# Load local settings from the YAML file
local_wp_path=$(yq e ".local.wordpress_path" $CONFIG_FILE)
# Load remote settings from the YAML file
remote_db_name=$(yq e ".${remote_env}.database.name" $CONFIG_FILE)
remote_db_user=$(yq e ".${remote_env}.database.user" $CONFIG_FILE)
remote_db_password=$(yq e ".${remote_env}.database.password" $CONFIG_FILE)
remote_db_host=$(yq e ".${remote_env}.database.host" $CONFIG_FILE)
remote_ssh_user=$(yq e ".${remote_env}.ssh.user" $CONFIG_FILE)
remote_ssh_host=$(yq e ".${remote_env}.ssh.host" $CONFIG_FILE)
remote_wp_path=$(yq e ".${remote_env}.wordpress_path" $CONFIG_FILE)
# Export the remote database and transfer it to the local machine
echo "Exporting database from $remote_env environment..."
ssh $remote_ssh_user@$remote_ssh_host "wp db export $remote_wp_path/remote-dump.sql --path=$remote_wp_path"
scp $remote_ssh_user@$remote_ssh_host:$remote_wp_path/remote-dump.sql ./local-dump.sql
# Import the remote database into the local environment
echo "Importing database into local environment..."
wp db import ./local-dump.sql --path=$local_wp_path
# Optionally run a search-replace to update URLs
remote_url=$(yq e ".${remote_env}.vhost" $CONFIG_FILE)
local_url=$(yq e ".local.vhost" $CONFIG_FILE)
wp search-replace "$remote_url" "$local_url" --path=$local_wp_path
echo "Database pull from $remote_env to local completed."
}
# Function to push database from local to remote
push_db() {
remote_env=$1
# Load local settings from the YAML file
local_wp_path=$(yq e ".local.wordpress_path" $CONFIG_FILE)
# Load remote settings from the YAML file
remote_db_name=$(yq e ".${remote_env}.database.name" $CONFIG_FILE)
remote_db_user=$(yq e ".${remote_env}.database.user" $CONFIG_FILE)
remote_db_password=$(yq e ".${remote_env}.database.password" $CONFIG_FILE)
remote_db_host=$(yq e ".${remote_env}.database.host" $CONFIG_FILE)
remote_ssh_user=$(yq e ".${remote_env}.ssh.user" $CONFIG_FILE)
remote_ssh_host=$(yq e ".${remote_env}.ssh.host" $CONFIG_FILE)
remote_wp_path=$(yq e ".${remote_env}.wordpress_path" $CONFIG_FILE)
# Export the local database
echo "Exporting local database..."
wp db export ./local-dump.sql --path=$local_wp_path
# Transfer the local database to the remote environment
echo "Transferring database to $remote_env environment..."
scp ./local-dump.sql $remote_ssh_user@$remote_ssh_host:$remote_wp_path/remote-dump.sql
# Import the database into the remote environment
echo "Importing database into remote environment..."
ssh $remote_ssh_user@$remote_ssh_host "wp db import $remote_wp_path/remote-dump.sql --path=$remote_wp_path"
# Optionally run a search-replace to update URLs
local_url=$(yq e ".local.vhost" $CONFIG_FILE)
remote_url=$(yq e ".${remote_env}.vhost" $CONFIG_FILE)
ssh $remote_ssh_user@$remote_ssh_host "wp search-replace '$local_url' '$remote_url' --path=$remote_wp_path"
echo "Database push from local to $remote_env completed."
}
# Function to determine push/pull and environment
sync_db() {
action=$1
environment=$2
check_environment $environment
if [[ "$action" == "pull" ]]; then
pull_db $environment
elif [[ "$action" == "push" ]]; then
push_db $environment
else
echo "Invalid action: $action. Use 'pull' or 'push'."
exit 1
fi
}
# Check if enough arguments were provided
if [ $# -lt 2 ]; then
echo "Usage: $0 {push|pull} {environment_name}"
exit 1
fi
# Call the sync function with provided arguments
sync_db $1 $2