diff options
-rwxr-xr-x | mysql/dump-all-to-homes.sh | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/mysql/dump-all-to-homes.sh b/mysql/dump-all-to-homes.sh new file mode 100755 index 0000000..3021293 --- /dev/null +++ b/mysql/dump-all-to-homes.sh @@ -0,0 +1,45 @@ +#!/bin/sh +# +# /etc/postgresql/dump-all-to-homes.sh +# Copyright 2003 Jonas Smedegaard <dr@jones.dk> +# +# $Id: dump-all-to-homes.sh,v 1.1 2003-03-05 05:57:01 jonas Exp $ +# +# Dump contents of all local Postgres databases to the homes of those +# with access to them. +# +# TODO: Rewrite to run as each normal user + +if [ -f /root/.my.cnf ]; then + rootpass=$(grep password /root/.my.cnf | awk -F= '{print $2}' | head -1 | sed 's/^ //g') +fi + +users=$(echo "select user from user where host = 'localhost';" | mysql --user=root -p$rootpass mysql | grep -vi '^user$' | sort -u) + +for user in $users; do + getent passwd $user | awk -F: '{print $6}' | grep -q "^/home/$user$" || continue + if [ -d /home/$user ]; then + if [ -d "/home/$user/mysql_dump" ]; then + if [ ! -O "/home/$user/mysql_dump" ]; then + echo "ERROR: Wrong permissions for /home/$user/mysql_dump" + exit 1 + fi + chgrp "$user" "/home/$user/mysql_dump" + else + install -d -o root -g "$user" -m 0750 "/home/$user/mysql_dump" + fi + else + echo "ERROR: Directory /home/$user doesn't exist" + exit 1 + fi + databases=$(echo "select db from db where host = 'localhost' and user = '$user' and select_priv = 'Y';" | mysql --user=root -p$rootpass mysql | grep -vi '^db$' | sed 's/%$//g' | sort -u) + for database in $databases; do + #TODO: Check againt `echo "show databases" | mysql -uroot -p$rootpass | grep -vi '^Database$';` + if [ -f /home/$user/mysql_dump/$database ]; then + mv /home/$user/mysql_dump/$database /home/$user/mysql_dump/$database.bak + fi + mysqldump -c --add-drop-table -uroot -p$rootpass "$database" > "/home/$user/mysql_dump/$database" + chmod 0640 "/home/$user/mysql_dump/$database" + chgrp $user "/home/$user/mysql_dump/$database" + done +done |