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