1#!/bin/sh 2# 3# smbtar script - front end to smbclient 4# 5# Authors: Martin.Kraemer <Martin.Kraemer@mch.sni.de> 6# and Ricky Poulten (ricky@logcam.co.uk) 7# 8# (May need to change shell to ksh for HPUX or OSF for better getopts) 9# 10# sandy nov 3 '98 added -a flag 11# 12# Richard Sharpe, added -c 'tarmode full' so that we back up all files to 13# fix a bug in clitar when a patch was added to stop system and hidden files 14# being backed up. 15 16case $0 in 17 # when called by absolute path, assume smbclient is in the same directory 18 /*) 19 SMBCLIENT="`dirname $0`/smbclient";; 20 *) # you may need to edit this to show where your smbclient is 21 SMBCLIENT="smbclient";; 22esac 23 24# These are the default values. You could fill them in if you know what 25# you're doing, but beware: better not store a plain text password! 26server="" 27service="backup" # Default: a service called "backup" 28password="" 29username=$LOGNAME # Default: same user name as in *nix 30verbose="2>/dev/null" # Default: no echo to stdout 31log="-d 2" 32newer="" 33newerarg="" 34blocksize="" 35blocksizearg="" 36clientargs="-c 'tarmode full'" 37tarcmd="c" 38tarargs="" 39cdcmd="\\" 40tapefile=${TAPE-tar.out} 41 42Usage(){ 43 ex=$1 44 shift 45echo >&2 "Usage: `basename $0` [<options>] [<include/exclude files>] 46Function: backup/restore a Windows PC directories to a local tape file 47Options: (Description) (Default) 48 -r Restore from tape file to PC Save from PC to tapefile 49 -i Incremental mode Full backup mode 50 -a Reset archive bit mode Don't reset archive bit 51 -v Verbose mode: echo command Don't echo anything 52 -s <server> Specify PC Server $server 53 -p <password> Specify PC Password $password 54 -x <share> Specify PC Share $service 55 -X Exclude mode Include 56 -N <newer> File for date comparison `set -- $newer; echo $2` 57 -b <blocksize> Specify tape's blocksize `set -- $blocksize; echo $2` 58 -d <dir> Specify a directory in share $cdcmd 59 -l <log> Specify a Samba Log Level `set -- $log; echo $2` 60 -u <user> Specify User Name $username 61 -t <tape> Specify Tape device $tapefile 62" 63 echo >&2 "$@" 64 exit $ex 65} 66 67# echo Params count: $# 68 69# DEC OSF AKA Digital UNIX does not seem to return a value in OPTIND if 70# there are no command line params, so protect us against that ... 71if [ $# = 0 ]; then 72 73 Usage 2 "Please enter a command line parameter!" 74 75fi 76 77while getopts riavl:b:d:N:s:p:x:u:Xt: c; do 78 case $c in 79 r) # [r]estore to Windows (instead of the default "Save from Windows") 80 tarcmd="x" 81 ;; 82 i) # [i]ncremental 83 tarargs=${tarargs}g 84 ;; 85 a) # [a]rchive 86 tarargs=${tarargs}a 87 ;; 88 l) # specify [l]og file 89 log="-d $OPTARG" 90 case "$OPTARG" in 91 [0-9]*) ;; 92 *) echo >&2 "$0: Error, log level not numeric: -l $OPTARG" 93 exit 1 94 esac 95 ;; 96 d) # specify [d]irectory to change to in server's share 97 cdcmd="$OPTARG" 98 ;; 99 N) # compare with a file, test if [n]ewer 100 if [ -f $OPTARG ]; then 101 newer=$OPTARG 102 newerarg="N" 103 else 104 echo >&2 $0: Warning, $OPTARG not found 105 fi 106 ;; 107 X) # Add exclude flag 108 tarargs=${tarargs}X 109 ;; 110 s) # specify [s]erver's share to connect to - this MUST be given. 111 server="$OPTARG" 112 ;; 113 b) # specify [b]locksize 114 blocksize="$OPTARG" 115 case "$OPTARG" in 116 [0-9]*) ;; 117 *) echo >&2 "$0: Error, block size not numeric: -b $OPTARG" 118 exit 1 119 esac 120 blocksizearg="b" 121 ;; 122 p) # specify [p]assword to use 123 password="$OPTARG" 124 ;; 125 x) # specify windows [s]hare to use 126 service="$OPTARG" 127 ;; 128 t) # specify [t]apefile on local host 129 tapefile="$OPTARG" 130 ;; 131 u) # specify [u]sername for connection 132 username="$OPTARG" 133 ;; 134 v) # be [v]erbose and display what's going on 135 verbose="" 136 ;; 137 '?') # any other switch 138 Usage 2 "Invalid switch specified - abort." 139 ;; 140 esac 141done 142 143shift `expr $OPTIND - 1` 144 145if [ "$server" = "" ] || [ "$service" = "" ]; then 146 Usage 1 "No server or no service specified - abort." 147fi 148 149# if the -v switch is set, the echo the current parameters 150if [ -z "$verbose" ]; then 151 echo "server is $server" 152# echo "share is $service" 153 echo "share is $service\\$cdcmd" 154 echo "tar args is $tarargs" 155# echo "password is $password" # passwords should never be sent to screen 156 echo "tape is $tapefile" 157 echo "blocksize is $blocksize" 158fi 159 160tarargs=${tarargs}${blocksizearg}${newerarg} 161 162eval $SMBCLIENT "'\\\\$server\\$service'" "'$password'" -U "'$username'" \ 163-E -N $log -D "'$cdcmd'" ${clientargs} \ 164-T${tarcmd}${tarargs} $blocksize $newer $tapefile '${1+"$@"}' $verbose 165