#!/bin/sh # vim: set sw=4 ts=8 sts=4 et ft=sh fenc=us-ascii ff=unix tw=74 : # (c) 2008 Robert Weiler . # This script is released under the MIT license. # # A script for mirroring a FTP server (or just a subdirectory) to your # local harddisk using rsync. To use this script you'll need: # * root privileges # * curlftpfs # * rsync # ########################################################################## # prints an usage message usage() { echo "`basename $0` [arguments]" echo echo "REQUIRED ARGUMENTS" echo " -s SERVER the DNS name or IP of the server to access" echo " -d LOCALDIR the directory to save files to" echo echo "OPTIONAL ARGUMENTS" echo " -f FTPDIR the directory on SERVER to mirror" echo " -b BANDWITH maximum bandwith to use (KB/s)" } # quit if the script is not called by root if [ 'root' != "`whoami`" ] ; then echo "Error: This script may only be run by root." exit 1 fi # usage if no arguments were given if [ 0 -eq $# ] ; then usage exit 1 fi # set defaults for all available options FTPSERVER="" LOCALDIR="" FTPDIR="" BWLIMIT="100000000" # parse command line options while getopts ":s:d:f:b:" FLAG ; do case "$FLAG" in s) FTPSERVER="$OPTARG" ;; d) LOCALDIR="$OPTARG" ;; f) FTPDIR="$OPTARG" ;; b) BWLIMIT="$OPTARG" ;; *) usage exit 1 ;; esac done # check if all required arguments were given if [ -z "$FTPSERVER" ] ; then echo "Error: Missing argument (-s)." exit 2 fi if [ -z "$LOCALDIR" ] ; then echo "Error: Missing argument (-l)." exit 2 fi # create a temporary directory for mounting the server MOUNTDIR="`mktemp`" rm "$MOUNTDIR" mkdir "$MOUNTDIR" # mount the FTP server as a local directory curlftpfs -r -s "$FTPSERVER" "$MOUNTDIR" if [ 0 -ne $? ] ; then echo "Error: Could not mount FTP server." rmdir "$MOUNTDIR" exit 3 fi # list the directory that shall be mirrored to test the connection ls -R "$MOUNTDIR/$FTPDIR" >/dev/null 2>&1 if [ 0 -ne $? ] ; then echo "Error: Could not read from the server." umount "$MOUNTDIR" rmdir "$MOUNTDIR" exit 4 fi # start mirroring rsync -a --progress --stats --ignore-errors --delete-after \ --bwlimit="$BWLIMIT" "$MOUNTDIR/$FTPDIR/" "$LOCALDIR" if [ 0 -ne $? ] ; then echo "Warning: rsync reported at least one error." fi # unmount the server, remove the mountpoint umount "$MOUNTDIR" rmdir "$MOUNTDIR" # that's it exit 0