bugfix
[scripts/scripts.git] / backup.sh
1 #!/bin/bash
2
3 # variables
4 remote_host=right-hand
5 remote_dir=/mnt/big/backup
6 local_mount=/mnt/nfs
7 local_backupdir=/mnt/bunker/backup
8
9 # initializing some variables
10 backup_date=`date | awk '{ print $3"."$2"."$6 }'`
11 host=`hostname`
12
13 mounted=0
14 res=1
15 if [ -d $remote_dir ] ; then
16         backup_dir=/mnt/big/backup
17         res=0
18 else
19         if [ -d $local_backupdir ] ; then
20                 backup_dir=$local_backupdir
21                 res=0
22         else
23                 mkdir -p $local_mount
24                 mount -tnfs ${remote_host}:${remote_dir} $local_mount
25                 res=$?
26                 backup_dir=$local_mount
27                 mounted=1
28         fi
29 fi
30
31 if [ "$res" != "0" ] ; then
32         echo "something failed detrmining the backup dir $backup_dir ..."
33         exit
34 fi
35
36 # which method to use?
37 if [ "$1" = "tar" ] ; then
38         method="tar"
39 elif [ "$1" = "dir" ] ; then 
40         method="dir"
41 elif [ -z "$1" ] ; then
42         method="tar"
43 else
44         echo "aborting!"
45         echo "usage: $0 [tar|dir] (-backup_dir /foo/bar)"
46         echo 
47         echo "tar: creates an archive called backup_${backup_date}.tar.bz2"
48         echo "dir: creates just the directory backup_${backup_date}"
49         exit 0
50 fi
51
52 if [ ! -z "$2" ] ; then
53         if [ "$2" = "-backup_dir" -a -d $3 ] ; then
54                 echo "using $3 as a backup directory ..."
55                 backup_dir=$3
56         fi
57 fi
58
59 # set kernel version(s) to store their .config
60 if [ -d /usr/src/linux ] ; then
61         kernel_num=`ls /usr/src/ | grep '^linux-' | sed 's/linux-//g'`
62 fi
63
64 # creating backup
65 if [ -d ${backup_dir}/${host}-backup-${backup_date} ] ; then
66         echo "u already did a backup today! remove it manually first!"
67         [ ! "$host" = "hackstation" ] && umount /mnt/nfs
68         exit 0
69 fi
70
71 mkdir $backup_dir/${host}-backup-${backup_date} && cd ${backup_dir}/${host}-backup-${backup_date}
72
73
74 #config stuff
75 echo "config stuff:"
76 mkdir ./${host}-conf && cd ./${host}-conf 
77
78 for i in $kernel_num; do
79         if [ -f /usr/src/linux-${i}/.config ] ; then
80                 echo "copying /usr/src/linux/.config"
81                 cp /usr/src/linux-${i}/.config ./$host-kernel-${i}-config
82         fi
83 done
84
85 mkdir ./etc
86 for i in init.d conf opt ppp cups udev cron.d; do
87         if [ -d /etc/$i ] ; then
88                 echo "copying /etc/$i"
89                 cp -r /etc/$i ./etc
90         fi
91 done
92
93 for i in dhcpd.conf fstab hosts host.conf hosts.allow hosts.deny lilo.conf \
94                 profile sendmail.cf resolv.conf exports fb.modes inetd.conf \
95                 xinetd.conf mp3user mp3db.conf modules.conf named.conf \
96                 modprobe.devfs modprobe.conf devfsd.conf crontab ethers; do
97         if [ -f /etc/$i ] ; then
98         echo "copying /etc/$i"
99         cp /etc/$i ./etc
100         fi
101 done
102
103 if [ -f /etc/X11/XF86Config ] ; then
104         mkdir -p ./etc/X11
105         echo "copying /etc/X11/XF86Config*"
106         cp /etc/X11/XF86Config* ./etc/X11
107 fi
108 if [ -f /etc/X11/xorg.conf ] ; then
109         mkdir -p ./etc/X11
110         echo "copying /etc/X11/xorg.conf*"
111         cp /etc/X11/xorg.conf* ./etc/X11
112 fi
113
114
115 # specific config stuff
116
117 # gate specific config stuff!
118 if [ "$host" = "gate" ] ; then
119         
120         # mail config stuff
121         if [ -d /etc/mail ] ; then
122                 echo "copying /etc/mail directory"
123                 cp -r /etc/mail ./etc
124         fi
125
126         # mlsit procmail
127         if [ -d /home/mlist ] ; then
128                 echo "copying mlist procmail config"
129                 cp -r /home/mlist mlsit_pm_config
130         fi
131
132         # irc stuff 
133         mkdir -p ./ircd
134         [ -f /etc/opt/unrealircd/unrealircd.conf ] && \
135                 cp /etc/opt/unrealircd/unrealircd.conf ./ircd
136         
137         # minimalist stuff 
138         mkdir -p ./minimalist
139         [ -f /etc/opt/minimalist/minimalist.conf ] && \
140                 cp /etc/opt/minimalist/minimalist.conf ./minimalist
141 fi
142
143 # right-hand specific config stuff
144 if [ "$host" = "right-hand" ] ; then
145         if [ -d /var/named ] ; then
146                 mkdir ./named
147                 echo "copying dns data"
148                 cp -r /var/named/* ./named
149         fi
150         if [ -f /etc/opt/apache*/httpd.conf ] ; then
151                 mkdir ./apache
152                 cp /etc/opt/apache*/httpd.conf ./apache
153         fi
154 fi
155
156
157 # data stuff
158 echo "data stuff:"
159
160 # data backup (specific)
161 cd ${backup_dir}/${host}-backup-${backup_date}
162
163 # gate specific data 
164 if [ "$host" = "gate" ] ; then
165         
166         # mail stuff
167         for i in `ls -A /var/mail`; do
168                 mkdir -p mail_$i
169                 echo "copying mail stuff"
170                 cp /var/mail/$i ./mail_$i
171         done
172 fi
173
174 # right-hand data
175 if [ "$host" = "right-hand" ] ; then
176         mkdir ./chroot
177         for i in www cvs; do
178                 if [ -d /chroot/$i ] ; then
179                         echo "copying /chroot/$i"
180                         tar -cf ./chroot/$i.tar /chroot/$i
181                 fi
182         done
183 fi
184
185 # hackstation / mobile / hdw / sparc specific data
186 if [ "$host" = "hackstation" -o "$host" = "mobile" -o "$host" = "hdw" -o "$host" = "sparc" -o "$host" = "compaq" ] ; then
187
188         # home of hackbard
189         mkdir -p ./home/hackbard
190         echo -n "copying home ..."
191         for i in temp test; do
192                 echo -n " $i,"
193                 [ -d /home/hackbard/$i ] && \
194                         cp -r /home/hackbard/$i ./home/hackbard
195         done
196         for i in xinitrc_evil xinitrc_wm .xinitrc GNUstep .Xauthority \
197                         .Xdefaults .XHkeys .signature .directfbrc .bashrc \
198                         sp12 .licq .irssi; do
199                 echo -n " $i,"
200                 [ -e /home/hackbard/$i ] && \
201                         cp -r /home/hackbard/$i ./home/hackbard
202         done
203         [ -d /home/hackbard/.mozilla ] &&
204                 cp /home/hackbard/.mozilla/*/*/bookmarks.html \
205                         mozilla-bookmarks.html
206         [ -d /home/hackbard/.ssh ] && cp -r /home/hackbard/.ssh ./home/hackbard
207         echo " done."
208         
209         # tftpboot
210         if [ -d /tftpboot ] ; then
211                 mkdir -p ./tftpboot
212                 echo "copying tftpboot stuff"
213                 cp -r /tftpboot/* ./tftpboot
214         fi
215
216         # wlan configs
217         for i in hunz_prism hunz_lucent roman_orinoco.sh; do
218                 if [ -f /root/${i}.sh ]; then
219                         mkdir -p ./root
220                         cp /root/${i}.sh ./root
221                 fi
222         done
223
224 fi
225
226
227 # data stuff (none specific)
228
229 # backing up myself
230 cp -r $HOME/scripts ./
231
232
233 # finished copying stuff, entering last backup procedure now :)))
234
235 cd $backup_dir
236
237 if [ "$method" = "tar" ] ; then
238         echo "creating tar"
239         tar cf ./${host}-backup-${backup_date}.tar \
240                                 ./${host}-backup-${backup_date}
241         rm -r ./${host}-backup-${backup_date}
242         bzip2 ./${host}-backup-${backup_date}.tar
243 fi
244
245
246 # end! umounting nfs shared backup directory
247 if [ "$mounted" = "1" ] ; then
248         echo "umounting nfs backup - dir"
249         cd $HOME && umount $local_mount
250 fi
251
252 echo "done ..."