bugfix
[scripts/scripts.git] / backup.sh
1 #!/bin/bash
2
3
4 # initializing some variables
5 backup_date=`date | awk '{ print $3"."$2"."$6 }'`
6 host=`hostname`
7
8
9 # set the backup directory
10 if [ "$host" = "gate" ] ; then
11         mount -tnfs hackstation:/mnt/lfs/backup /mnt/nfs
12         backup_dir="/mnt/nfs"
13 elif [ "$host" = "sparc" ] ; then
14         mount -tnfs hackstation:/mnt/lfs/backup /mnt/nfs
15         backup_dir="/mnt/nfs"
16 elif [ "$host" = "mobile" ] ; then
17         mount -tnfs hackstation:/mnt/lfs/backup /mnt/nfs
18         backup_dir="/mnt/nfs"
19 elif [ "$host" = "right-hand" ] ; then
20         mount -tnfs hackstation:/mnt/lfs/backup /mnt/nfs
21         backup_dir="/mnt/nfs"
22 elif [ "$host" = "hackstation" ] ; then 
23         backup_dir="/mnt/lfs/backup"
24 elif [ "$host" = "hdw" ] ; then
25         mount -tnfs hackstation:/mnt/lfs/backup /mnt/nfs
26         backup_dir="/mnt/nfs"
27 elif [ "$host" = "compaq" ] ; then
28         mount -tnfs hackstation:/mnt/lfs/backup /mnt/nfs
29         backup_dir="/mnt/nfs"
30 else
31         echo "pls adept the script for $host ..."
32         exit 1
33 fi
34
35 if [ ! -z "$2" ] ; then
36 if [ "$2" = "-backup_dir" -a -d $3 ] ; then
37         echo "using $3 as a backup directory ..."
38         backup_dir=$3
39 fi
40 fi
41
42 # set kernel version(s) to store their .config
43 if [ -d /usr/src/linux ] ; then
44         kernel_num=`ls /usr/src/ | grep '^linux-' | sed 's/linux-//g'`
45 fi
46
47
48 # which method to use?
49 if [ "$1" = "tar" ] ; then
50         method="tar"
51 elif [ "$1" = "dir" ] ; then 
52         method="dir"
53 elif [ -z "$1" ] ; then
54         method="tar"
55 else
56         echo "aborting!"
57         echo "usage: $0 [tar|dir] (-backup_dir /foo/bar)"
58         echo 
59         echo "tar: creates an archive called backup_${backup_date}.tar.bz2"
60         echo "dir: creates just the directory backup_${backup_date}"
61         exit 0
62 fi
63
64
65 # creating backup
66 if [ -d ${backup_dir}/${host}-backup-${backup_date} ] ; then
67         echo "u already did a backup today! remove it manually first!"
68         [ ! "$host" = "hackstation" ] && umount /mnt/nfs
69         exit 0
70 fi
71
72 mkdir $backup_dir/${host}-backup-${backup_date} && cd ${backup_dir}/${host}-backup-${backup_date}
73
74
75 #config stuff
76 echo "config stuff:"
77 mkdir ./${host}-conf && cd ./${host}-conf 
78
79 for i in $kernel_num; do
80         if [ -f /usr/src/linux-${i}*/.config ] ; then
81                 echo "copying /usr/src/linux/.config"
82                 cp /usr/src/linux-${i}*/.config ./$host-kernel-${i}-config
83         fi
84 done
85
86 mkdir ./etc
87 for i in init.d conf opt ppp cups; do
88         if [ -d /etc/$i ] ; then
89                 echo "copying /etc/$i"
90                 cp -r /etc/$i ./etc
91         fi
92 done
93
94 for i in dhcpd.conf fstab hosts host.conf hosts.allow hosts.deny lilo.conf \
95                 profile sendmail.cf resolv.conf exports fb.modes inetd.conf \
96                 xinetd.conf mp3user mp3db.conf; 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
109
110 # specific config stuff
111
112 # gate specific config stuff!
113 if [ "$host" = "gate" ] ; then
114         
115         # mail config stuff
116         if [ -d /etc/mail ] ; then
117                 echo "copying /etc/mail directory"
118                 cp -r /etc/mail ./etc
119         fi
120
121         # irc stuff 
122         mkdir -p ./ircd
123         [ -f /etc/opt/unrealircd/unrealircd.conf ] && \
124                 cp /etc/opt/unrealircd/unrealircd.conf ./ircd
125 fi
126
127 # data stuff
128 echo "data stuff:"
129
130 # data backup (specific)
131 cd ${backup_dir}/${host}-backup-${backup_date}
132
133 # gate specific data 
134 if [ "$host" = "gate" ] ; then
135         
136         # mail stuff
137         for i in `ls -A /var/mail`; do
138                 mkdir -p mail_$i
139                 echo "copying mail stuff"
140                 cp /var/mail/$i ./mail_$i
141         done
142 fi
143
144 # right-hand data
145 if [ "$host" = "right-hand" ] ; then
146         mkdir ./chroot
147         for i in www cvs hdw; do
148                 if [ -d /chroot/$i ] ; then
149                         echo "copying /chroot/$i"
150                         tar -cf ./chroot/$i.tar /chroot/$i
151                 fi
152         done
153 fi
154
155 # hackstation / mobile / hdw / sparc specific data
156 if [ "$host" = "hackstation" -o "$host" = "mobile" -o "$host" = "hdw" -o "$host" = "sparc" -o "$host" = "left-hand" ] ; then
157
158         # home of hackbard
159         mkdir -p ./home/hackbard
160         echo "copying home"
161         for i in software store; do
162                 [ -d /home/hackbard/$i ] && \
163                         cp -r /home/hackbard/$i ./home/hackbard
164         done
165         if [ "$host" = "hackstation" ]; then
166                 [ -d /home/hackbard/projects ] && \
167                         cp -r /home/hackbard/projects ./home/hackbard
168         fi
169         for i in xinitrc_evil xinitrc_wm .xinitrc GNUstep .Xauthority \
170                         .Xdefaults .signature .directfbrc; do
171                 [ -f /home/hackbard/$i ] && \
172                         cp -r /home/hackbard/$i ./home/hackbard
173         done
174         [ -d /home/hackbard/.ssh ] && cp -r /home/hackbard/.ssh ./home/hackbard
175         
176         # tftpboot
177         if [ -d /tftpboot ] ; then
178                 mkdir -p ./tftpboot
179                 echo "copyinf tftpboot stuff"
180                 cp -r /tftpboot/* ./tftpboot
181         fi
182
183         # hunz config
184         for i in prism lucent; do
185                 if [ -f /root/hunz_${i}.sh ]; then
186                         mkdir -p ./root
187                         cp /root/hunz_${i}.sh ./root
188                 fi
189         done
190
191 fi
192
193
194 # data stuff (none specific)
195
196 # backing up myself
197 cp -r $HOME/scripts ./
198
199
200 # finished copying stuff, entering last backup procedure now :)))
201
202 cd $backup_dir
203
204 if [ "$method" = "tar" ] ; then
205         echo "creating tar"
206         tar cf ./${host}-backup-${backup_date}.tar \
207                                 ./${host}-backup-${backup_date}
208         rm -r ./${host}-backup-${backup_date}
209         bzip2 ./${host}-backup-${backup_date}.tar
210 fi
211
212
213 # end! umounting nfs shared backup directory
214 if [ "$host" != "hackstation" ] ; then
215         echo "umounting nfs backup - dir"
216         cd $HOME && umount /mnt/nfs
217 fi
218
219 echo "done ..."