make_catalog_backup 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/bin/sh
  2. #
  3. # This script dumps your Bacula catalog in ASCII format
  4. # It works for MySQL, SQLite, and PostgreSQL
  5. #
  6. # $1 is the name of the database to be backed up and the name
  7. # of the output file (default = bacula).
  8. # $2 is the user name with which to access the database
  9. # (default = bacula).
  10. # $3 is the password with which to access the database or "" if no password
  11. # (default ""). WARNING!!! Passing the password via the command line is
  12. # insecure and should not be used since any user can display the command
  13. # line arguments and the environment using ps. Please consult your
  14. # MySQL or PostgreSQL manual for secure methods of specifying the
  15. # password.
  16. # $4 is the host on which the database is located
  17. # (default "")
  18. # $5 is the type of database
  19. #
  20. #
  21. default_db_type=sqlite3
  22. #
  23. # See if the fifth argument is a valid backend name.
  24. # If so the user overrides the default database backend.
  25. #
  26. if [ $# -ge 5 ]; then
  27. case $5 in
  28. sqlite3)
  29. db_type=$5
  30. ;;
  31. mysql)
  32. db_type=$5
  33. ;;
  34. postgresql)
  35. db_type=$5
  36. ;;
  37. ingres)
  38. db_type=$5
  39. ;;
  40. *)
  41. ;;
  42. esac
  43. fi
  44. #
  45. # If no new db_type is gives use the default db_type.
  46. #
  47. if [ -z "${db_type}" ]; then
  48. db_type="${default_db_type}"
  49. fi
  50. cd /var/lib/bacula
  51. rm -f $1.sql
  52. case ${db_type} in
  53. sqlite3)
  54. BINDIR=/usr/bin
  55. echo ".dump" | ${BINDIR}/sqlite3 $1.db >$1.sql
  56. ;;
  57. mysql)
  58. BINDIR=
  59. if test $# -gt 2; then
  60. MYSQLPASSWORD=" --password=$3"
  61. else
  62. MYSQLPASSWORD=""
  63. fi
  64. if test $# -gt 3; then
  65. MYSQLHOST=" --host=$4"
  66. else
  67. MYSQLHOST=""
  68. fi
  69. ${BINDIR}/mysqldump -u ${2}${MYSQLPASSWORD}${MYSQLHOST} -f --opt $1 >$1.sql
  70. ;;
  71. postgresql)
  72. BINDIR=
  73. if test $# -gt 2; then
  74. PGPASSWORD=$3
  75. export PGPASSWORD
  76. fi
  77. if test $# -gt 3; then
  78. PGHOST=" --host=$4"
  79. else
  80. PGHOST=""
  81. fi
  82. # you could also add --compress for compression. See man pg_dump
  83. exec ${BINDIR}/pg_dump -c $PGHOST -U $2 $1 >$1.sql
  84. ;;
  85. esac
  86. #
  87. # To read back a MySQL database use:
  88. # cd /var/lib/bacula
  89. # rm -f ${BINDIR}/../var/bacula/*
  90. # mysql <bacula.sql
  91. #
  92. # To read back a SQLite database use:
  93. # cd /var/lib/bacula
  94. # rm -f bacula.db
  95. # sqlite bacula.db <bacula.sql
  96. #
  97. # To read back a PostgreSQL database use:
  98. # cd /var/lib/bacula
  99. # dropdb bacula
  100. # createdb bacula -T template0 -E SQL_ASCII
  101. # psql bacula <bacula.sql
  102. #