check-blacklist.sh: Fix potential false positive
[mgsautils.git] / mysql / backup.sh
1 #!/bin/sh
2 passfile=$1
3 cfgfile=$(readlink -f $2)
4 stordir=$(readlink -f $3)
5 tempdir=/tmp/mysqlbak_$$
6 currdat=$(date +%Y%m%d)
7 archive=$(hostname -s)
8 tarname=mysqlbak_${archive}_${currdat}
9
10 case $(uname) in
11 FreeBSD) statres=$(stat -f %Sp/%Su ${passfile});;
12 Linux) statres=$(stat -c %A/%U ${passfile});;
13 *) echo Unknown OS.; exit 1;;
14 esac
15
16 if [ "${statres}" != "-r--------/$(whoami)" ]
17 then
18 echo "Password file must have 400 permissions and must be owned by you! (And it must exist, by the way.)"
19 exit 1
20 fi
21 dbpass=$(cat ${passfile})
22
23 umask 027
24 mkdir ${tempdir}
25 cd ${tempdir}
26 cat ${cfgfile} | while read db
27 do
28 sqlfile=mysqlbak_${db}_${currdat}.sql
29 if ! mysqldump --user root --password=${dbpass} --databases ${db} > ${sqlfile};
30 then
31 echo "Failed to dump ${db} to ${sqlfile}."
32 rm ${sqlfile}
33 fi
34 done
35
36 if tar cvf ${tarname}.tar *.sql;
37 then
38 rm *.sql
39 bzip2 --best *.tar
40 mv ${tarname}.tar.bz2 ${stordir}
41
42 cd ${stordir}
43 rmdir ${tempdir}
44
45 shift 3
46 for target in "$@"
47 do
48 scp ${tarname}.tar.bz2 ${target}
49 done
50 else
51 echo "Failed to create archive: ${tarname}."
52 echo "Cleaning up..."
53 cd ${stordir}
54 rm -rv ${tempdir}
55 exit 1
56 fi