#!/bin/bash # Copyright (c) 2009 Florian Reitmeir # Copyright (c) 2009 Peter Palfrader # # Permission is hereby granted, free of charge, to any person # obtaining a copy of this software and associated documentation # files (the "Software"), to deal in the Software without # restriction, including without limitation the rights to use, # copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the # Software is furnished to do so, subject to the following # conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR # OTHER DEALINGS IN THE SOFTWARE. # Author: Florian Reitmeir # E-mail: florian@reitmeir.org base_http=http://www.example.com/pub base_rsync=marvin.example.com:/var/www/www.example.com/htdocs/pub [ -e ~/.publish.cfg ] && . ~/.publish.cfg usage() { cat << EOF usage: $0 [ ...] copy the file to a server an report the URL. OPTIONS: -h Show this message -n no-do. Just print what would have been done EOF } uri_encode() { perl -MURI::Escape -ne 'print uri_escape($_)' } get_random() { head -c 8 /dev/urandom | base64 | tr '/+' '-_' | tr -d '=' } NODO="" while getopts "hn" OPTION do case $OPTION in h) usage exit ;; n) NODO="echo" ;; *) usage >&2 exit 1 ;; esac done shift $(($OPTIND - 1)) if [[ "$#" = 0 ]]; then echo "No files to copy" >&2 exit 2 fi d_date=$(date +'%Y-%m-%d') d_random=$(get_random) d_server="$d_date-$d_random" rsync_args="--partial --recursive --compress" d_server_http_base=$( echo -n "$d_server" | uri_encode ) if [ "$#" = 1 ]; then only_one=1 else only_one=0 echo "$base_http/$d_server_http_base/" fi while [ "$#" -gt 0 ]; do file="$1" shift if [ -d "$file" ] && [ "$only_one" = 1 ]; then trail="/" else trail="" fi $NODO rsync $rsync_args "$file$trail" $base_rsync"/$d_server/" echo "$base_http/$d_server_http_base/$( echo -n "$file" | uri_encode )" done