summaryrefslogtreecommitdiff
path: root/localdumpsql
diff options
context:
space:
mode:
authorJonas Smedegaard <dr@jones.dk>2003-05-20 03:42:55 +0000
committerJonas Smedegaard <dr@jones.dk>2003-05-20 03:42:55 +0000
commit7ab9a8d7f77901f4c05a0a611af7e4d3016e57b1 (patch)
treebfed72b80248948d86d88a41a25063262266c8f0 /localdumpsql
parent51aa2e226155fd8833928e552c0588f2de164379 (diff)
Rewrite to dump into backupdir for each database owner.
Diffstat (limited to 'localdumpsql')
-rwxr-xr-xlocaldumpsql68
1 files changed, 43 insertions, 25 deletions
diff --git a/localdumpsql b/localdumpsql
index 64b90b9..eefd277 100755
--- a/localdumpsql
+++ b/localdumpsql
@@ -1,9 +1,9 @@
#!/bin/bash
#
# /usr/local/sbin/localdumpsql
-# Copyright 2001-2002 Jonas Smedegaard <dr@jones.dk>
+# Copyright 2001-2003 Jonas Smedegaard <dr@jones.dk>
#
-# $Id: localdumpsql,v 1.4 2003-05-20 00:22:30 jonas Exp $
+# $Id: localdumpsql,v 1.5 2003-05-20 03:42:55 jonas Exp $
#
# MySQL/PostgreSQL maintenance script
#
@@ -13,15 +13,18 @@
# halt on errors
set -e
+# Be paranoid about access to created files
+umask 077
+
function usage() {
- echo "Usage: `basename $0` daily|weekly|monthly|<whatever> [mysql|postgres]"
- echo " If sqltype is missing, all are attempted"
+ echo "Usage: `basename $0` daily|weekly|monthly|<whatever> [mysql|postgres [<user> [<db>]]]"
+ echo " If sqltype, user and db are not provided, all are included"
echo " Tip: Automagically runs when symlinked to /etc/cron.{daily,weekly,monthly}/"
exit 1
}
# automagically configure when run from cron dirs
-case `dirname $0` in
+case `dirname "$0"` in
/etc/cron.daily)
stamp=daily
;;
@@ -32,11 +35,13 @@ case `dirname $0` in
stamp=monthly
;;
*)
- if [ $# -lt 1 -o $# -gt 2 ]; then
+ if [ $# -lt 1 -o $# -gt 4 ]; then
usage
fi
- stamp=$1
- sqltypes=$2
+ stamp="$1"
+ sqltypes="$2"
+ users="$3"
+ databases="$4"
;;
esac
@@ -44,24 +49,25 @@ esac
[ -z $sqltypes ] && sqltypes="mysql postgres"
# Define paths
-targetdir=/var/local/backups/localhost
mysql_bin=/usr/bin/mysqldump
postgres_bin=/usr/lib/postgresql/bin/pg_dump
# Define routines
-function mysql_valid() { [ -x $mysql_bin ]; }
-function mysql_list_db() { mysql -uroot -e 'show databases' | grep -v '^Database$'; }
-function mysql_dump_db() { $mysql_bin -c --add-drop-table -uroot $1; }
+function gethome_user() { getent passwd "$1" | awk -F: '{print $6}' | head -n 1; }
+function getbackupdir_user() { echo "`gethome_user \"$1\"`/backup"; }
-function postgres_valid() { [ -x $postgres_bin -a -x /usr/lib/postgresql/bin/pg_ctl ]; }
-function postgres_list_db() { su -s /bin/sh postgres -c "/usr/bin/psql -t -c 'select datname from pg_database order by datname' -d template1 | sed -e 's/ //' | grep -v '^template[01]$'"; }
-function postgres_dump_db() { su -s /bin/sh postgres -c "$postgres_bin $1"; }
+function mysqlvalid() { [ -x $mysql_bin ]; }
+#function mysqlpasswd_user() { grep password "`gethome_user $1`"/.my.cnf | awk -F= '{print $2}' | awk '{print $1}' | head -1; }
+function mysqlusers() { mysql -uroot mysql -e "select User from user;" | grep -Ev '^(User|debian-sys-maint|root|.+-admin)$' | sort -u; }
+function mysqldb_user() { mysql -u root mysql -re "select Db from db where User='$1';" | grep -v '^Db$' | sort -u; }
+#function mysqldump_user_db() { $mysql_bin --opt --user="$1" --password="`mysqlpasswd_user $1`" "$2"; }
+#function mysqldump_user_db() { $mysql_bin --opt --user="root" --password="`mysqlpasswd_user root`" "$2"; }
+function mysqldump_user_db() { $mysql_bin --opt --user="root" "$2"; }
-# Exit if the directory isn't there
-if [ ! -d "$targetdir" ]; then
- echo "Directory $targetdir doesn't exist!"
- exit 1
-fi
+function postgresvalid() { [ -x $postgres_bin -a -x /usr/lib/postgresql/bin/pg_ctl ]; }
+function postgresusers() { echo "root"; } #FIXME
+function postgresdb_user() { su -s /bin/sh postgres -c "/usr/bin/psql -t -c 'select datname from pg_database order by datname' -d template1 | sed -e 's/ //' | grep -v '^template[01]$'"; }
+function postgresdump_user_db() { su -s /bin/sh postgres -c "$postgres_bin $2"; }
# Check for valid input
for sqltype in $sqltypes; do
@@ -75,11 +81,23 @@ for sqltype in $sqltypes; do
done
for sqltype in $sqltypes; do
- if ${sqltype}_valid; then
- for db in `${sqltype}_list_db`; do
- targetfile=$targetdir/dump_${stamp}_${db}.${sqltype}
- [ -f $targetfile ] && savelog -c 3 $targetfile >/dev/null
- ${sqltype}_dump_db $db > $targetfile
+ if ${sqltype}valid; then
+ [ $sqltype = "postgres" ] && users="root" #FIXME
+ [ -z "$users" ] && users="`${sqltype}users`"
+ for user in $users; do
+ home="`gethome_user \"$user\"`"
+ test -d "$home" || continue
+ targetdir="`getbackupdir_user \"$user\"`/$sqltype"
+ if [ ! -d "$targetdir" ]; then
+ mkdir -p "$targetdir"
+ chown $user "$targetdir"
+ fi
+ [ -z "$databases" ] && databases="`${sqltype}db_user \"$user\"`"
+ for db in $databases; do
+ targetfile="$targetdir/$stamp.$db.sql"
+ (set +e; ${sqltype}dump_user_db $user $db; set -e) > "$targetfile"
+ chown $user "$targetfile"
+ done
done
fi
done