| 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 | while [ "$i" -gt 1 ]; do |
|---|
| 54 | next_i=$(($i - 1)) |
|---|
| 55 | mv -f "${target_dir}/${db}.sql.${next_i}${ext}" \ |
|---|
| 56 | "${target_dir}/${db}.sql.${i}${ext}" 2>/dev/null |
|---|
| 57 | i=$next_i # loop should end here |
|---|
| 58 | done |
|---|
| 59 | mv -f "${target_dir}/${db}.sql${ext}" \ |
|---|
| 60 | "${target_dir}/${db}.sql.${i}${ext}" 2>/dev/null |
|---|
| 61 | if [ "$compressed" -eq 1 ]; then |
|---|
| 62 | mysqldump -h"$MYSQL_HOST" -u"$login" -p"$pass" "$db" | |
|---|
| 63 | gzip -c > "${target_dir}/${db}.sql${ext}" |
|---|
| 64 | else |
|---|
| 65 | mysqldump -h"$MYSQL_HOST" -u"$login" -p"$pass" "$db" \ |
|---|
| 66 | > "${target_dir}/${db}.sql" |
|---|
| 67 | fi |
|---|
| 68 | |
|---|
| 69 | IFS=" " |
|---|
| 70 | done |
|---|
| 71 | IFS="$old_ifs" |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | if [ "$1" = "daily" ]; then |
|---|
| 75 | # Daily : |
|---|
| 76 | mode=2 |
|---|
| 77 | else |
|---|
| 78 | # weekly: |
|---|
| 79 | mode=1 |
|---|
| 80 | fi |
|---|
| 81 | |
|---|
| 82 | /usr/bin/mysql -h"$MYSQL_HOST" -u"$MYSQL_USER" -p"$MYSQL_PASS" \ |
|---|
| 83 | "$MYSQL_DATABASE" -B << EOF | tail -n '+2' | dobck |
|---|
| 84 | SELECT login, pass, db, bck_history, bck_gzip, bck_dir |
|---|
| 85 | FROM db |
|---|
| 86 | WHERE bck_mode=$mode; |
|---|
| 87 | EOF |
|---|
| 88 | |
|---|
| 89 | # vim: et sw=4 |
|---|