different backups for primary and secondary
[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 # crontabs
115 if [ -d /var/spool/cron/crontabs ] ; then
116         echo "copying crontab files"
117         cp -r /var/spool/cron/crontabs .
118 fi
119
120
121 # specific config stuff
122
123 # gate specific config stuff!
124 if [ "$host" = "gate" ] ; then
125         
126         # mail config stuff
127         if [ -d /etc/mail ] ; then
128                 echo "copying /etc/mail directory"
129                 cp -r /etc/mail ./etc
130         fi
131
132         # mlsit procmail
133         if [ -d /home/mlist ] ; then
134                 echo "copying mlist procmail config"
135                 cp -r /home/mlist mlsit_pm_config
136         fi
137
138         # irc stuff 
139         mkdir -p ./ircd
140         [ -f /etc/opt/unrealircd/unrealircd.conf ] && \
141                 cp /etc/opt/unrealircd/unrealircd.conf ./ircd
142         
143         # minimalist stuff 
144         mkdir -p ./minimalist
145         [ -f /etc/opt/minimalist/minimalist.conf ] && \
146                 cp /etc/opt/minimalist/minimalist.conf ./minimalist
147 fi
148
149 # right-hand specific config stuff
150 if [ "$host" = "right-hand" ] ; then
151         if [ -d /var/named ] ; then
152                 mkdir ./named
153                 echo "copying dns data"
154                 cp -r /var/named/* ./named
155         fi
156         if [ -f /etc/opt/apache*/httpd.conf ] ; then
157                 mkdir ./apache
158                 cp /etc/opt/apache*/httpd.conf ./apache
159         fi
160 fi
161
162
163 # data stuff
164 echo "data stuff:"
165
166 # data backup (specific)
167 cd ${backup_dir}/${host}-backup-${backup_date}
168
169 # gate specific data 
170 if [ "$host" = "gate" ] ; then
171         
172         # mail stuff
173         for i in `ls -A /var/mail`; do
174                 mkdir -p mail_$i
175                 echo "copying mail stuff"
176                 cp /var/mail/$i ./mail_$i
177         done
178 fi
179
180 # right-hand data
181 if [ "$host" = "right-hand" ] ; then
182         mkdir ./chroot
183         for i in www cvs; do
184                 if [ -d /chroot/$i ] ; then
185                         echo "copying /chroot/$i"
186                         tar -cf ./chroot/$i.tar /chroot/$i
187                 fi
188         done
189 fi
190
191 # hackstation / mobile / hdw / sparc specific data
192 if [ "$host" = "hackstation" -o "$host" = "mobile" -o "$host" = "hdw" -o "$host" = "sparc" -o "$host" = "compaq" ] ; then
193
194         # home of hackbard
195         mkdir -p ./home/hackbard
196         echo -n "copying home ..."
197         for i in temp test; do
198                 echo -n " $i,"
199                 [ -d /home/hackbard/$i ] && \
200                         cp -r /home/hackbard/$i ./home/hackbard
201         done
202         for i in xinitrc_evil xinitrc_wm .xinitrc GNUstep .Xauthority \
203                         .Xdefaults .XHkeys .signature .directfbrc .bashrc \
204                         sp12 .licq .irssi; do
205                 echo -n " $i,"
206                 [ -e /home/hackbard/$i ] && \
207                         cp -r /home/hackbard/$i ./home/hackbard
208         done
209         [ -d /home/hackbard/.mozilla ] &&
210                 cp /home/hackbard/.mozilla/*/*/bookmarks.html \
211                         mozilla-bookmarks.html
212         [ -d /home/hackbard/.ssh ] && cp -r /home/hackbard/.ssh ./home/hackbard
213         echo " done."
214         
215         # tftpboot
216         if [ -d /tftpboot ] ; then
217                 mkdir -p ./tftpboot
218                 echo "copying tftpboot stuff"
219                 cp -r /tftpboot/* ./tftpboot
220         fi
221
222         # wlan configs
223         for i in hunz_prism hunz_lucent roman_orinoco.sh; do
224                 if [ -f /root/${i}.sh ]; then
225                         mkdir -p ./root
226                         cp /root/${i}.sh ./root
227                 fi
228         done
229
230 fi
231
232
233 # data stuff (none specific)
234
235 # backing up myself
236 cp -r $HOME/scripts ./
237
238
239 # finished copying stuff, entering last backup procedure now :)))
240
241 cd $backup_dir
242
243 if [ "$method" = "tar" ] ; then
244         echo "creating tar"
245         tar cf ./${host}-backup-${backup_date}.tar \
246                                 ./${host}-backup-${backup_date}
247         rm -r ./${host}-backup-${backup_date}
248         bzip2 ./${host}-backup-${backup_date}.tar
249 fi
250
251
252 # end! umounting nfs shared backup directory
253 if [ "$mounted" = "1" ] ; then
254         echo "umounting nfs backup - dir"
255         cd $HOME && umount $local_mount
256 fi
257
258 echo "done ..."