Makefile for secure file and database synchronization
Published on 2007-12-01 22:37:24.
Description
This is a Makefile for secure file and MySQL database content synchronization between localhost and a remote server.It uses "rsync" for syncing files, "scp" for transferring database dumps, "mysql" for importing database dumps and "mysqldump" for dumping database contents.
If you do any web development you will find this extremely useful.
Contents
[Hide]Content
1
2 #
3 # $Id: Makefile 75 2007-07-15 17:18:25Z zapotek $
4 #
5 # Makefile for syncing files and MySQL databases between local and remote server
6 #
7 # Written by Zapotek [zapotek@segfault.gr]
8 #############################################
9
10
11
12 # remote settings
13 ##################
14 # remote SSH port
15 R_SSHPORT= 22
16 # remote user name
17 R_USER= root
18 # remote server
19 R_SERVER= domain-name.com
20 # remote directory
21 R_DIR= ~/public_html/
22 # remote database
23 R_DB= my_database_name
24 # remote database user
25 R_DB_USER= db_user
26 # remote database password for the above user
27 R_DB_PWD= mypassword
28
29 # local settings
30 #################
31 # local dir
32 L_DIR= .
33 # output format for "rsync"
34 L_OUT_FORMAT= %i %n
35 # ssh configuration
36 L_RSH= /usr/bin/ssh -Cq -p $(R_SSHPORT)
37 # local database
38 L_DB= my_database_name
39 # local database user
40 L_DB_USER= root
41 # remote database password for the above user
42 L_DB_PWD=
43 # scp configuration
44 L_SCP= /usr/bin/scp -C -P $(R_SSHPORT)
45
46 #
47 # the default target for make, it just prints usage info
48 #
49
50 all:
51 @echo Makefile for file and database synchronization
52 @echo Author: Zapotek [zapotek@segfault.gr]
53 @echo
54 @echo Your make targets are:
55 @echo ' o file_get gets files that have been changed/added on the server'
56 @echo ' o file_put same as "file_get" except that it sends files'
57 @echo ' o file_sync it does a "file_get" and then a "file_put"'
58 @echo ' o db_get gets the remote DB contents'
59 @echo ' o db_put same as "db_get" except that it sends the DB contents'
60 @echo ' o all_get gets the remote files & DB contents'
61 @echo ' o all_put same as "all_get" except that it sends stuff'
62
63
64 #
65 # get remote files and append/add them if they don't exist
66 # but don't overwrite identical files
67 #
68
69 file_get:
70 @echo Getting files from server...
71
72 @rsync --cvs-exclude \
73 --perms \
74 --checksum \
75 --recursive \
76 --archive \
77 --verbose \
78 --update \
79 --compress \
80 --backup \
81 --human-readable \
82 -e '$(L_RSH)' \
83 --stats \
84 --out-format='$(L_OUT_FORMAT)' \
85 $(R_USER)@$(R_SERVER):$(R_DIR) $(L_DIR)
86
87 @echo ...done receiving files.
88
89 #
90 # same as above but instead of getting files, send them
91 #
92 file_put:
93 @echo Sending files to server...
94
95 @rsync --cvs-exclude \
96 --perms \
97 --checksum \
98 --recursive \
99 --archive \
100 --verbose \
101 --update \
102 --compress \
103 --backup \
104 --human-readable \
105 -e '$(L_RSH)' \
106 --stats \
107 --out-format='$(L_OUT_FORMAT)' \
108 --include '.htaccess' \
109 --exclude 'Makefile' \
110 $(L_DIR) $(R_USER)@$(R_SERVER):$(R_DIR)
111
112 @echo ...done trasmitting files.
113
114 #
115 # get DB contents from remote server
116 #
117 db_get:
118 @echo Getting DB contents from server...
119 @echo
120
121 @echo -n Dumping remote DB to SQL file
122 @$(L_RSH) $(R_USER)@$(R_SERVER) \
123 'mysqldump \
124 --user=$(R_DB_USER) \
125 --password=$(R_DB_PWD) \
126 $(R_DB) > /tmp/$(R_DB).sql'
127 @echo ' [Done]'
128
129 @echo -n Getting remote SQL file from server
130 @$(L_SCP) $(R_USER)@$(R_SERVER):/tmp/$(R_DB).sql /tmp/$(R_DB).sql
131 @echo ' [Done]'
132
133 @echo -n Populating local DB
134 @mysql \
135 --user=$(L_DB_USER) \
136 --password=$(L_DB_PWD) \
137 $(L_DB) < /tmp/$(R_DB).sql
138 @echo ' [Done]'
139
140 #
141 # send local DB contents to remote server
142 #
143 db_put:
144 @echo Sending DB contents to server...
145 @echo
146
147 @echo -n Dumping to SQL file
148 @mysqldump \
149 -v \
150 --user=$(L_DB_USER) \
151 --password=$(L_DB_PWD) \
152 $(L_DB) > /tmp/$(L_DB).sql
153 @echo ' [Done]'
154
155 @echo Sending SQL file to server
156 @$(L_SCP) /tmp/$(L_DB).sql $(R_USER)@$(R_SERVER):/tmp/$(L_DB).sql
157 @echo ' [Done]'
158
159 @echo -n Populating remote DB
160 @$(L_RSH) $(R_USER)@$(R_SERVER) \
161 'mysql \
162 --user=$(R_DB_USER) \
163 --password=$(R_DB_PWD) \
164 $(R_DB) < /tmp/$(L_DB).sql'
165 @echo ' [Done]'
166
167
168 #
169 # sync files
170 # first get new files/changes from the remote location,
171 # then push local files/changes to the remote location
172 #
173 file_sync: file_get file_put
174
175 #
176 # download files and remote DB content
177 #
178 all_get: file_get db_get
179
180 #
181 # upload files and local DB content
182 #
183 all_put: file_put db_put
184
185
2 #
3 # $Id: Makefile 75 2007-07-15 17:18:25Z zapotek $
4 #
5 # Makefile for syncing files and MySQL databases between local and remote server
6 #
7 # Written by Zapotek [zapotek@segfault.gr]
8 #############################################
9
10
11
12 # remote settings
13 ##################
14 # remote SSH port
15 R_SSHPORT= 22
16 # remote user name
17 R_USER= root
18 # remote server
19 R_SERVER= domain-name.com
20 # remote directory
21 R_DIR= ~/public_html/
22 # remote database
23 R_DB= my_database_name
24 # remote database user
25 R_DB_USER= db_user
26 # remote database password for the above user
27 R_DB_PWD= mypassword
28
29 # local settings
30 #################
31 # local dir
32 L_DIR= .
33 # output format for "rsync"
34 L_OUT_FORMAT= %i %n
35 # ssh configuration
36 L_RSH= /usr/bin/ssh -Cq -p $(R_SSHPORT)
37 # local database
38 L_DB= my_database_name
39 # local database user
40 L_DB_USER= root
41 # remote database password for the above user
42 L_DB_PWD=
43 # scp configuration
44 L_SCP= /usr/bin/scp -C -P $(R_SSHPORT)
45
46 #
47 # the default target for make, it just prints usage info
48 #
49
50 all:
51 @echo Makefile for file and database synchronization
52 @echo Author: Zapotek [zapotek@segfault.gr]
53 @echo
54 @echo Your make targets are:
55 @echo ' o file_get gets files that have been changed/added on the server'
56 @echo ' o file_put same as "file_get" except that it sends files'
57 @echo ' o file_sync it does a "file_get" and then a "file_put"'
58 @echo ' o db_get gets the remote DB contents'
59 @echo ' o db_put same as "db_get" except that it sends the DB contents'
60 @echo ' o all_get gets the remote files & DB contents'
61 @echo ' o all_put same as "all_get" except that it sends stuff'
62
63
64 #
65 # get remote files and append/add them if they don't exist
66 # but don't overwrite identical files
67 #
68
69 file_get:
70 @echo Getting files from server...
71
72 @rsync --cvs-exclude \
73 --perms \
74 --checksum \
75 --recursive \
76 --archive \
77 --verbose \
78 --update \
79 --compress \
80 --backup \
81 --human-readable \
82 -e '$(L_RSH)' \
83 --stats \
84 --out-format='$(L_OUT_FORMAT)' \
85 $(R_USER)@$(R_SERVER):$(R_DIR) $(L_DIR)
86
87 @echo ...done receiving files.
88
89 #
90 # same as above but instead of getting files, send them
91 #
92 file_put:
93 @echo Sending files to server...
94
95 @rsync --cvs-exclude \
96 --perms \
97 --checksum \
98 --recursive \
99 --archive \
100 --verbose \
101 --update \
102 --compress \
103 --backup \
104 --human-readable \
105 -e '$(L_RSH)' \
106 --stats \
107 --out-format='$(L_OUT_FORMAT)' \
108 --include '.htaccess' \
109 --exclude 'Makefile' \
110 $(L_DIR) $(R_USER)@$(R_SERVER):$(R_DIR)
111
112 @echo ...done trasmitting files.
113
114 #
115 # get DB contents from remote server
116 #
117 db_get:
118 @echo Getting DB contents from server...
119 @echo
120
121 @echo -n Dumping remote DB to SQL file
122 @$(L_RSH) $(R_USER)@$(R_SERVER) \
123 'mysqldump \
124 --user=$(R_DB_USER) \
125 --password=$(R_DB_PWD) \
126 $(R_DB) > /tmp/$(R_DB).sql'
127 @echo ' [Done]'
128
129 @echo -n Getting remote SQL file from server
130 @$(L_SCP) $(R_USER)@$(R_SERVER):/tmp/$(R_DB).sql /tmp/$(R_DB).sql
131 @echo ' [Done]'
132
133 @echo -n Populating local DB
134 @mysql \
135 --user=$(L_DB_USER) \
136 --password=$(L_DB_PWD) \
137 $(L_DB) < /tmp/$(R_DB).sql
138 @echo ' [Done]'
139
140 #
141 # send local DB contents to remote server
142 #
143 db_put:
144 @echo Sending DB contents to server...
145 @echo
146
147 @echo -n Dumping to SQL file
148 @mysqldump \
149 -v \
150 --user=$(L_DB_USER) \
151 --password=$(L_DB_PWD) \
152 $(L_DB) > /tmp/$(L_DB).sql
153 @echo ' [Done]'
154
155 @echo Sending SQL file to server
156 @$(L_SCP) /tmp/$(L_DB).sql $(R_USER)@$(R_SERVER):/tmp/$(L_DB).sql
157 @echo ' [Done]'
158
159 @echo -n Populating remote DB
160 @$(L_RSH) $(R_USER)@$(R_SERVER) \
161 'mysql \
162 --user=$(R_DB_USER) \
163 --password=$(R_DB_PWD) \
164 $(R_DB) < /tmp/$(L_DB).sql'
165 @echo ' [Done]'
166
167
168 #
169 # sync files
170 # first get new files/changes from the remote location,
171 # then push local files/changes to the remote location
172 #
173 file_sync: file_get file_put
174
175 #
176 # download files and remote DB content
177 #
178 all_get: file_get db_get
179
180 #
181 # upload files and local DB content
182 #
183 all_put: file_put db_put
184
185
| Code statistics | ||||
|---|---|---|---|---|
| Physical lines | Code lines | Comment lines | Empty lines | Size |
| 184 [ 100.00% ] | 99 [ 53.80% ] | 52 [ 28.26% ] | 33 [ 17.93% ] | 4066 bytes |
| [ Download ] |