#!/bin/bash # # /usr/local/bin/rsync-websites.sh # # Mirror static website content to another server using rsync and SSH. # # Copyright 2012 - Jean-Sebastien Morisset - http://surniaulula.com/ # # This script is free software; you can redistribute it and/or modify it under # the terms of the GNU General Public License as published by the Free Software # Foundation; either version 3 of the License, or (at your option) any later # version. # # This script is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details # at http://www.gnu.org/licenses/. www_dir="/export/www" rsync_opts="--recursive --times --omit-dir-times --links --delete" static_srv="static.YOUR-DOMAIN-NAME.com" function RsyncStatic () { [ -n "$visual" ] && echo -e "\nrsync $2...\n" # # Exclude all cache folders, except those that contain images, like # gallery/cache/ and blogroll-links-favicons/cache/. # # Exclude all PHP scripts, except for those under bwp-minify/min/, # which may provide minified files on the remote (PHP-enabled) server # by also using the CDN Linker plugin. Protect files under # bwp-minify/cache/ from being deleted on the remote server since they # are needed by the PHP scripts under bwp-minify/min/. # rsync --rsh="ssh -x" --perms --delete-excluded \ --filter='P wp-content/plugins/bwp-minify/cache/**' \ --filter='+ wp-content/plugins/bwp-minify/min/**' \ --filter='+ wp-content/plugins/blogroll-links-favicons/cache/**' \ --filter='+ wp-content/gallery/cache/**' \ --filter='- **/cache/**' \ --filter='- *-modified' \ --filter='- *-new' \ --filter='- *-orig' \ --filter='- *.php' \ --filter='- *.po' \ --filter='- *.mo' \ --filter='- *.pot' \ --filter='- *.sql' \ --filter='- *.swp' \ --filter='- screenshot-*.jpg' \ --filter='- screenshot-*.png' \ $rsync_opts "$1" "$2" } # read command line opts while : do for arg in "$@" do case $arg in -h|--help) echo "purpose: rsync content to remote websites." echo " syntax: $0 [--help|--visual|--test]" exit 0 ;; -t|--test) rsync_opts="$rsync_opts --dry-run" shift 1 ;; -v|--visual) visual="1" rsync_opts="$rsync_opts --verbose" shift 1 ;; -*) echo "error: unrecognized command line argument." exit 1 ;; *) args[$(( i++ ))]="$1" shift 1 ;; esac continue 2 done break done # reset $1, $2, etc. with left-over paramters set -- "${args[@]}" # sync static server for sitedir in $www_dir/* do for subdir in wordpress do if [ -d "$sitedir/$subdir/" ] then sitename="${sitedir##*/}" RsyncStatic "$sitedir/$subdir/" \ "$static_srv:www/$sitename/$subdir/" fi done done