# # $Id: Makefile 75 2007-07-15 17:18:25Z zapotek $ # # Makefile for syncing files and MySQL databases between local and remote server # # Written by Zapotek [zapotek@segfault.gr] ############################################# # remote settings ################## # remote SSH port R_SSHPORT= 22 # remote user name R_USER= root # remote server R_SERVER= domain-name.com # remote directory R_DIR= ~/public_html/ # remote database R_DB= my_database_name # remote database user R_DB_USER= db_user # remote database password for the above user R_DB_PWD= mypassword # local settings ################# # local dir L_DIR= . # output format for "rsync" L_OUT_FORMAT= %i %n # ssh configuration L_RSH= /usr/bin/ssh -Cq -p $(R_SSHPORT) # local database L_DB= my_database_name # local database user L_DB_USER= root # remote database password for the above user L_DB_PWD= # scp configuration L_SCP= /usr/bin/scp -C -P $(R_SSHPORT) # # the default target for make, it just prints usage info # all: @echo Makefile for file and database synchronization @echo Author: Zapotek [zapotek@segfault.gr] @echo @echo Your make targets are: @echo ' o file_get gets files that have been changed/added on the server' @echo ' o file_put same as "file_get" except that it sends files' @echo ' o file_sync it does a "file_get" and then a "file_put"' @echo ' o db_get gets the remote DB contents' @echo ' o db_put same as "db_get" except that it sends the DB contents' @echo ' o all_get gets the remote files & DB contents' @echo ' o all_put same as "all_get" except that it sends stuff' # # get remote files and append/add them if they don't exist # but don't overwrite identical files # file_get: @echo Getting files from server... @rsync --cvs-exclude \ --perms \ --checksum \ --recursive \ --archive \ --verbose \ --update \ --compress \ --backup \ --human-readable \ -e '$(L_RSH)' \ --stats \ --out-format='$(L_OUT_FORMAT)' \ $(R_USER)@$(R_SERVER):$(R_DIR) $(L_DIR) @echo ...done receiving files. # # same as above but instead of getting files, send them # file_put: @echo Sending files to server... @rsync --cvs-exclude \ --perms \ --checksum \ --recursive \ --archive \ --verbose \ --update \ --compress \ --backup \ --human-readable \ -e '$(L_RSH)' \ --stats \ --out-format='$(L_OUT_FORMAT)' \ --include '.htaccess' \ --exclude 'Makefile' \ $(L_DIR) $(R_USER)@$(R_SERVER):$(R_DIR) @echo ...done trasmitting files. # # get DB contents from remote server # db_get: @echo Getting DB contents from server... @echo @echo -n Dumping remote DB to SQL file @$(L_RSH) $(R_USER)@$(R_SERVER) \ 'mysqldump \ --user=$(R_DB_USER) \ --password=$(R_DB_PWD) \ $(R_DB) > /tmp/$(R_DB).sql' @echo ' [Done]' @echo -n Getting remote SQL file from server @$(L_SCP) $(R_USER)@$(R_SERVER):/tmp/$(R_DB).sql /tmp/$(R_DB).sql @echo ' [Done]' @echo -n Populating local DB @mysql \ --user=$(L_DB_USER) \ --password=$(L_DB_PWD) \ $(L_DB) < /tmp/$(R_DB).sql @echo ' [Done]' # # send local DB contents to remote server # db_put: @echo Sending DB contents to server... @echo @echo -n Dumping to SQL file @mysqldump \ -v \ --user=$(L_DB_USER) \ --password=$(L_DB_PWD) \ $(L_DB) > /tmp/$(L_DB).sql @echo ' [Done]' @echo Sending SQL file to server @$(L_SCP) /tmp/$(L_DB).sql $(R_USER)@$(R_SERVER):/tmp/$(L_DB).sql @echo ' [Done]' @echo -n Populating remote DB @$(L_RSH) $(R_USER)@$(R_SERVER) \ 'mysql \ --user=$(R_DB_USER) \ --password=$(R_DB_PWD) \ $(R_DB) < /tmp/$(L_DB).sql' @echo ' [Done]' # # sync files # first get new files/changes from the remote location, # then push local files/changes to the remote location # file_sync: file_get file_put # # download files and remote DB content # all_get: file_get db_get # # upload files and local DB content # all_put: file_put db_put