| 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 | # Get mysql user and password : |
|---|
| 31 | . /etc/alternc/local.sh |
|---|
| 32 | |
|---|
| 33 | function dobck { |
|---|
| 34 | read RECORD |
|---|
| 35 | while [ "$RECORD" ] |
|---|
| 36 | do |
|---|
| 37 | # login, pass, bck_history, bck_gzip, bck_dir |
|---|
| 38 | LOGIN=`echo "$RECORD"|cut -f 1` |
|---|
| 39 | PASS=`echo "$RECORD"|cut -f 2` |
|---|
| 40 | DB=`echo "$RECORD"|cut -f 3` |
|---|
| 41 | COUNT=`echo "$RECORD"|cut -f 4` |
|---|
| 42 | COMPRESSED=`echo "$RECORD"|cut -f 5` |
|---|
| 43 | TARGET_DIR=`echo "$RECORD"|cut -f 6` |
|---|
| 44 | |
|---|
| 45 | if [ $COMPRESSED -eq 1 ] |
|---|
| 46 | then |
|---|
| 47 | EXT=".gz" |
|---|
| 48 | fi |
|---|
| 49 | i=$COUNT |
|---|
| 50 | while [ $i -gt 1 ] |
|---|
| 51 | do |
|---|
| 52 | next_i=$(($i - 1)) |
|---|
| 53 | mv -f "$TARGET_DIR/$DB.sql.$next_i$EXT" "$TARGET_DIR/$DB.sql.$i$EXT" 2>/dev/null |
|---|
| 54 | i=$next_i # loop should end here |
|---|
| 55 | done |
|---|
| 56 | mv -f "$TARGET_DIR/$DB.sql$EXT" "$TARGET_DIR/$DB.sql.$i$EXT" 2>/dev/null |
|---|
| 57 | if [ $COMPRESSED -eq 1 ] |
|---|
| 58 | then |
|---|
| 59 | mysqldump -u"$LOGIN" -p"$PASS" "$DB" | gzip -c > "$TARGET_DIR/$DB.sql$EXT" |
|---|
| 60 | else |
|---|
| 61 | mysqldump -u"$LOGIN" -p"$PASS" "$DB" >"$TARGET_DIR/$DB.sql" |
|---|
| 62 | fi |
|---|
| 63 | |
|---|
| 64 | read RECORD |
|---|
| 65 | done |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | if [ "X$1" == "Xdaily" ]; then |
|---|
| 69 | # Daily : |
|---|
| 70 | mode=2 |
|---|
| 71 | else |
|---|
| 72 | # weekly: |
|---|
| 73 | mode=1 |
|---|
| 74 | fi |
|---|
| 75 | |
|---|
| 76 | /usr/bin/mysql -u"$MYSQL_USER" -p"$MYSQL_PASS" "$MYSQL_DATABASE" -B -e "SELECT login, pass, db, bck_history, bck_gzip, bck_dir FROM db WHERE bck_mode=$mode;" |grep -v "^login" | dobck |
|---|
| 77 | |
|---|