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