summaryrefslogtreecommitdiff
path: root/localdumpsql
blob: a38a7a906977dcdb09a088af9cb2f2c1e39c999d (plain)
  1. #!/bin/bash
  2. #
  3. # /usr/local/sbin/localdumpsql
  4. # Copyright 2001-2003 Jonas Smedegaard <dr@jones.dk>
  5. #
  6. # $Id: localdumpsql,v 1.13 2003-06-01 16:38:20 jonas Exp $
  7. #
  8. # MySQL/PostgreSQL maintenance script
  9. #
  10. # Automagically runs when symlinked to /etc/cron.{daily,weekly,monthly}/
  11. #
  12. # halt on errors
  13. set -e
  14. # Be paranoid about access to created files
  15. umask 277
  16. function usage() {
  17. echo "Usage: `basename $0` daily|weekly|monthly|<whatever> [mysql|postgres [<user> [<db>]]]"
  18. echo " If sqltype, user and db are not provided, all are included"
  19. echo " Tip: Automagically runs when symlinked to /etc/cron.{daily,weekly,monthly}/"
  20. exit 1
  21. }
  22. # automagically configure when run from cron dirs
  23. case `dirname "$0"` in
  24. /etc/cron.daily)
  25. stamp=""
  26. ;;
  27. /etc/cron.weekly)
  28. stamp="weekly"
  29. ;;
  30. /etc/cron.monthly)
  31. stamp="monthly"
  32. ;;
  33. *)
  34. if [ $# -lt 1 -o $# -gt 4 ]; then
  35. usage
  36. fi
  37. stamp="$1"
  38. sqltypes="$2"
  39. users="$3"
  40. databases="$4"
  41. ;;
  42. esac
  43. # Default is all sqltypes
  44. [ -z $sqltypes ] && sqltypes="mysql postgres"
  45. # Define paths
  46. mysql_bin=/usr/bin/mysqldump
  47. postgres_bin=/usr/lib/postgresql/bin/pg_dump
  48. # Define routines
  49. function gethome_user() { getent passwd "$1" | awk -F: '{print $6}' | head -n 1; }
  50. function getbackupdir_user() { echo "`gethome_user \"$1\"`/backup"; }
  51. function mysqlvalid() { [ -x $mysql_bin ]; }
  52. #function mysqlpasswd_user() { grep password "`gethome_user $1`"/.my.cnf | awk -F= '{print $2}' | awk '{print $1}' | head -1; }
  53. function mysql_users() { if [ -n "$1" ]; then echo "$1"; else mysql -uroot mysql -e "select User from user;" | grep -Ev '^(User|debian-sys-maint|root|.+-admin)$' | sort -u; fi; }
  54. function mysql_db_user() { if [ -n "$1" ]; then echo "$1"; else mysql -u root mysql -re "select Db from db where User='$2';" | grep -v '^Db$' | sort -u; fi; }
  55. #function mysqldump_user_db() { $mysql_bin --opt --user="$1" --password="`mysqlpasswd_user $1`" "$2"; }
  56. #function mysqldump_user_db() { $mysql_bin --opt --user="root" --password="`mysqlpasswd_user root`" "$2"; }
  57. function mysqldump_user_db() { $mysql_bin --opt --user="root" "$2"; }
  58. function postgresvalid() { [ -x $postgres_bin -a -x /usr/lib/postgresql/bin/pg_ctl ]; }
  59. function postgres_users() { echo "root"; } #FIXME
  60. function postgres_db_user() { if [ -n "$1" ]; then echo "$1"; else 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]$'"; fi; }
  61. function postgresdump_user_db() { su -s /bin/sh postgres -c "$postgres_bin $2"; }
  62. # Check for valid input
  63. for sqltype in $sqltypes; do
  64. case "$sqltype" in
  65. mysql|postgres)
  66. ;;
  67. *)
  68. usage
  69. ;;
  70. esac
  71. done
  72. for sqltype in $sqltypes; do
  73. if ${sqltype}valid; then
  74. for user in `${sqltype}_users "$users"`; do
  75. home="`gethome_user \"$user\"`"
  76. test -d "$home" || continue
  77. for dir in "`getbackupdir_user \"$user\"`" "`getbackupdir_user \"$user\"`/$sqltype"; do
  78. targetdir="$dir"
  79. if [ ! -d "$targetdir" ]; then
  80. mkdir "$targetdir"
  81. fi
  82. chown "$user": "$targetdir"
  83. chmod u=rX,go= "$targetdir"
  84. done
  85. for db in `${sqltype}_db_user "$databases" "$user"`; do
  86. targetfile="$targetdir/$db${stamp:+.$stamp}.sql"
  87. (set +e; ${sqltype}dump_user_db "$user" "$db"; set -e) > "$targetfile"
  88. chown "$user": "$targetfile"
  89. chmod u=r,go= "$targetfile"
  90. done
  91. done
  92. fi
  93. done