Guest User

Untitled

a guest
Mar 11th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. #!/bin/bash
  2. # ITEMAN MySQL Backup - A command to backup specified MySQL databases
  3. # Copyright (c) 2009 ITEMAN, Inc. All rights reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions are met:
  7. #
  8. # * Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above copyright
  11. # notice, this list of conditions and the following disclaimer in the
  12. # documentation and/or other materials provided with the distribution.
  13. #
  14. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  15. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  18. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. # POSSIBILITY OF SUCH DAMAGE.
  25.  
  26. MYSQL_USER=root
  27. MYSQL_PASSWORD=PASSWORD
  28. BACKUP_DIRECTORY=
  29. BACKUP_EXPIRATION=3
  30. DATABASES=(
  31. )
  32.  
  33. if [ "x$BACKUP_DIRECTORY" = "x" ]; then
  34. echo "$0: BACKUP_DIRECTORY must be specified, exit"
  35. exit 1
  36. fi
  37.  
  38. for DATABASE in ${DATABASES[@]}; do
  39. BACKUP_DATE=`/bin/date +%Y%m%d%H%M%S`
  40. BACKUP_FILE="$BACKUP_DIRECTORY/$DATABASE-$BACKUP_DATE"
  41. echo -n "$0: Creating the backup file [ $BACKUP_FILE ] ... "
  42. mysqldump --user=$MYSQL_USER --password=$MYSQL_PASSWORD $DATABASE > $BACKUP_FILE
  43. RETVAL=$?
  44. if [ $RETVAL -ne 0 ]; then
  45. echo "failed, skip this project"
  46. rm -f $BACKUP_FILE
  47. continue
  48. fi
  49. echo "done"
  50.  
  51. echo -n "$0: Compressing the backup file [ $BACKUP_FILE ] ... "
  52. bzip2 $BACKUP_FILE
  53. RETVAL=$?
  54. if [ $RETVAL -ne 0 ]; then
  55. echo "failed, skip this project"
  56. continue
  57. fi
  58. echo "done"
  59.  
  60. COMPRESSED_BACKUP_FILE="$BACKUP_FILE.bz2"
  61. echo -n "$0: Changing the permission [ $COMPRESSED_BACKUP_FILE ] ... "
  62. chmod 400 $COMPRESSED_BACKUP_FILE
  63. RETVAL=$?
  64. if [ $RETVAL -ne 0 ]; then
  65. echo "failed, skip this project"
  66. continue
  67. fi
  68. echo "done"
  69.  
  70. echo "$0: Backup successfully completed"
  71. done
  72.  
  73. echo -n "$0: Removing the expired backup files ... "
  74. find $BACKUP_DIRECTORY -maxdepth 1 -type f -mtime +$BACKUP_EXPIRATION -exec rm -f '{}' \;
  75. RETVAL=$?
  76. if [ $RETVAL -ne 0 ]; then
  77. echo "failed, exit"
  78. exit 1
  79. fi
  80. echo "done"
  81.  
  82. exit 0
  83.  
  84. # Local Variables:
  85. # mode: shell-script
  86. # coding: iso-8859-1
  87. # tab-width: 4
  88. # indent-tabs-mode: nil
  89. # End:
Add Comment
Please, Sign In to add comment