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