root/alternc/tags/0.9.7/src/sqlbackup.sh

Revision 1796, 3.0 kB (checked in by anarcat, 2 years ago)

skip non-existent directories so that misconfigured backups don't break other user's backups

  • Property svn:executable set to *
Line 
1 #!/bin/sh
2
3 # $Id: sqlbackup.sh,v 1.9 2005/05/04 16:20:14 anarcat Exp $
4 # ----------------------------------------------------------------------
5 # AlternC - Web Hosting System
6 # Copyright (C) 2002 by the AlternC Development Team.
7 # http://alternc.org/
8 # ----------------------------------------------------------------------
9 # Based on:
10 # Valentin Lacambre's web hosting softwares: http://altern.org/
11 # ----------------------------------------------------------------------
12 # LICENSE
13 #
14 # This program is free software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License (GPL)
16 # as published by the Free Software Foundation; either version 2
17 # of the License, or (at your option) any later version.
18 #
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 # GNU General Public License for more details.
23 #
24 # To read the license please visit http://www.gnu.org/copyleft/gpl.html
25 # ----------------------------------------------------------------------
26 # Original Author of file: Benjamin Sonntag - 2003-03-23
27 # Purpose of file: MySQL Database backup shell script for AlternC
28 # ----------------------------------------------------------------------
29
30 set -e
31
32 # Get mysql user and password :
33 . /etc/alternc/local.sh
34
35 function dobck {
36     local ext
37     local i
38     local old_ifs
39
40     # mysql -B uses tab as a separator between fields, so we have to mess
41     # with IFS in order to get the correct behaviour
42     old_ifs="$IFS"
43     IFS="       "
44     while read login pass db count compressed target_dir; do
45         IFS="$old_ifs"
46
47         if [ "$compressed" -eq 1 ]; then
48             ext=".gz"
49         else
50             ext=""
51         fi
52         i="$count"
53        if [ ! -d "$target_dir" ] ; then
54                echo "$target_dir is not a directory, skipping" >&2
55                continue
56        fi
57         while [ "$i" -gt 1 ]; do
58           next_i=$(($i - 1))
59           mv -f "${target_dir}/${db}.sql.${next_i}${ext}" \
60                 "${target_dir}/${db}.sql.${i}${ext}" 2>/dev/null || true
61           i=$next_i # loop should end here
62         done
63         mv -f "${target_dir}/${db}.sql${ext}" \
64               "${target_dir}/${db}.sql.${i}${ext}" 2>/dev/null || true
65         if [ "$compressed" -eq 1 ]; then
66             mysqldump -h"$MYSQL_HOST" -u"$login" -p"$pass" "$db"  --add-drop-table --allow-keywords -Q -f -q -a -e |
67                 gzip -c > "${target_dir}/${db}.sql${ext}"
68         else
69             mysqldump -h"$MYSQL_HOST" -u"$login" -p"$pass" "$db"  --add-drop-table --allow-keywords -Q -f -q -a -e \
70                 > "${target_dir}/${db}.sql"
71         fi
72
73         IFS="   "
74     done
75     IFS="$old_ifs"
76 }
77
78 if [ "$1" = "daily" ]; then
79     # Daily :
80     mode=2
81 else
82     # weekly:
83     mode=1
84 fi
85
86 /usr/bin/mysql -h"$MYSQL_HOST" -u"$MYSQL_USER" -p"$MYSQL_PASS" \
87     "$MYSQL_DATABASE" -B << EOF | tail -n '+2' | dobck
88 SELECT login, pass, db, bck_history, bck_gzip, bck_dir
89   FROM db
90  WHERE bck_mode=$mode;
91 EOF
92
93 # vim: et sw=4
94
Note: See TracBrowser for help on using the browser.