Advertisement
Guest User

Untitled

a guest
Jan 26th, 2017
598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # This script will backup one or more mySQL databases
  4. # and then optionally email them and/or FTP them
  5.  
  6. # This script will create a different backup file for each database by day of the week
  7. # i.e. 1-dbname1.sql.gz for database=dbname1 on Monday (day=1)
  8. # This is a trick so that you never have more than 7 days worth of backups on your FTP server.
  9. # as the weeks rotate, the files from the same day of the prev week are overwritten.
  10.  
  11. ############################################################
  12. #===> site-specific variables - customize for your site
  13.  
  14. # List all of the MySQL databases that you want to backup in here,
  15. # each seperated by a space
  16. databases="db_name"
  17.  
  18. # Directory where you want the backup files to be placed
  19. backupdir=/home/username/backup
  20.  
  21. # MySQL dump command, use the full path name here
  22. mysqldumpcmd=/usr/bin/mysqldump
  23.  
  24. # MySQL Username and password
  25. userpassword=" --user=user_name --password=pass_here"
  26.  
  27. # MySQL dump options
  28. dumpoptions=" --quick --add-drop-table --add-locks --extended-insert --lock-tables"
  29.  
  30. # Unix Commands
  31. gzip=/bin/gzip
  32. uuencode=/usr/bin/uuencode
  33. mail=/bin/mail
  34.  
  35. # Send Backup? Would you like the backup emailed to you?
  36. # Set to "y" if you do
  37. sendbackup="n"
  38. subject="Backup is done"
  39. mailto="email@domain.tld"
  40.  
  41. #===> site-specific variables for FTP Use FQDN or IP
  42. ftpbackup="y"
  43. ftpserver="ftp.domain.tld"
  44. ftpuser="ftp_username"
  45. ftppasswd="ftp_pass"
  46. # If you are keeping the backups in a subdir to your FTP root
  47. ftpdir="/"
  48.  
  49. #===> END site-specific variables - customize for your site
  50. ############################################################
  51.  
  52. # Get the Day of the Week (0-6)
  53. # This allows to save one backup for each day of the week
  54. # Just alter the date command if you want to use a timestamp
  55. DOW=`date +%w`
  56.  
  57. # Create our backup directory if not already there
  58. mkdir -p ${backupdir}
  59. if [ ! -d ${backupdir} ]
  60. then
  61. echo "Not a directory: ${backupdir}"
  62. exit 1
  63. fi
  64.  
  65. # Dump all of our databases
  66. echo "Dumping MySQL Databases"
  67. for database in $databases
  68. do
  69. $mysqldumpcmd $userpassword $dumpoptions $database > ${backupdir}/${DOW}-${database}.sql
  70. done
  71.  
  72. # Compress all of our backup files
  73. echo "Compressing Dump Files"
  74. for database in $databases
  75. do
  76. rm -f ${backupdir}/${DOW}-${database}.sql.gz
  77. $gzip ${backupdir}/${DOW}-${database}.sql
  78. done
  79.  
  80. # Send the backups via email
  81. if [ $sendbackup = "y" ]
  82. then
  83. for database in $databases
  84. do
  85. $uuencode ${backupdir}/${DOW}-${database}.sql.gz > ${backupdir}/${DOW}-${database}.sql.gz.uu
  86. $mail -s "$subject : $database" $mailto < ${backupdir}/${DOW}-${database}.sql.gz.uu
  87. done
  88. fi
  89.  
  90. # FTP it to the off-site server
  91. echo "FTP file to $ftpserver FTP server"
  92. if [ $ftpbackup = "y" ]
  93. then
  94. for database in $databases
  95. do
  96. echo "==> ${backupdir}/${DOW}-${database}.sql.gz"
  97. ftp -n $ftpserver <<EOF
  98. user $ftpuser $ftppasswd
  99. bin
  100. prompt
  101. cd $ftpdir
  102. lcd ${backupdir}
  103. put ${DOW}-${database}.sql.gz
  104. quit
  105. EOF
  106. done
  107. fi
  108.  
  109. # And we're done
  110. ls -l ${backupdir}
  111. echo "Dump Complete!"
  112. exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement