Guest User

shell.sh

a guest
May 22nd, 2017
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.30 KB | None | 0 0
  1. #!/bin/bash
  2. # Author: Anand Subramanian
  3. # Publisher: http://LinuXplained.com
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. #
  18.  
  19. #############BEGIN EDIT AREA######################
  20. # BELOW ARE SOME REQUIRED SETTINGS. CONFIGURE THEM PROPERLY BEFORE USING
  21. # THE SCRIPT
  22.  
  23. # DBHOSTNAME is the host name of the host that hosts yours MySQL database.
  24. # You can get this from GoDaddy. Just replace the entire contents after =
  25. # with your database host name.
  26. DBHOSTNAME=xxxxx.db.xxxxxxx.hostedresource.com
  27.  
  28. # DBUSERNAME is the username to access the MySQL database. Just replace
  29. # the word databaseusername below with your username. No
  30. # quotes needed.
  31. DBUSERNAME=databaseusername
  32.  
  33. # DBPASSWORD is the password to access the MySQL database. Just replace
  34. # the word databasepassword below with your password database. No
  35. # quotes needed.
  36. DBPASSWORD=databasepassword
  37.  
  38. # DBNAME is the MySQL database that needs to be backed up. Typically, in
  39. # GoDaddy Linux Hosting this is the same as the DBUSERNAME
  40. DBNAME=databasename
  41.  
  42. # Path of the folder where backups will be stored. $HOME/html will
  43. # automatically put you in the hosting accounts root folder. GoDaddy MySQL
  44. # databases are normally stored in the _db_backups folder within your base
  45. # hosting folder. If the folder does not exist create it before running the
  46. # script. You can also create a subfolder within _db_backups folder if you
  47. # would like to backup the database to a separate folder. Alternatively,
  48. # you can backup to an entirely different folder of your choice. Whatever
  49. # you choose to do, ensure that the path is correctly specified below.
  50. BACKUPFOLDER=$HOME/html/_db_backups/backupfolder
  51.  
  52. # Should the script delete older files based on the conditions you set
  53. # below (Y or N - uppercase letters only). Choosing Y will maintain only
  54. # recent backups based on your settings below. Choosing N will keep all
  55. # the backups from the past.
  56. DELETEFILES=Y
  57.  
  58. # Do daily backups Y or N? (one uppercase letter only)
  59. DAILYBACKUP=Y
  60.  
  61. # Number of recent daily backups to keep. The default is 6 (Sun-Fri) with
  62. # Weekly backup on Sat.All previous backups will be deleted. This is
  63. # meaningless unless DAILYBACKUP is set to Y.
  64. NUMDAILYBACKUPS=6
  65.  
  66. # Do weekly backups Y or N? (one uppercase letter only). Weekly backups
  67. # are done on Saturdays.
  68. WEEKLYBACKUP=Y
  69.  
  70. # Number of recent weekly backups to keep. The default is 4. All previous
  71. # weekly backups will be deleted. This is meaningless unless WEEKLYBACKUP
  72. # is set to Y.
  73. NUMWEEKLYBACKUPS=4
  74.  
  75. # Do monthly backups Y or N? (one uppercase letter only). Monthly backups
  76. # are done on the last day of the month.
  77. MONTHLYBACKUP=Y
  78.  
  79. # Number of recent monthly backups to keep. The default is 2. All previous
  80. # monthly backups will be deleted. This is meaningless unless
  81. # MONTHLYBACKUP is set to Y.
  82. NUMMONTHLYBACKUPS=2
  83.  
  84. #############END EDIT AREA######################
  85. #
  86. # DO NOT EDIT ANYTHING BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING.
  87. # WHILE YOU CAN EDIT IT TO CUSTOMIZE HOW THE SCRIPT WORKS, DOING SO CAN
  88. # BREAK THE FUNCTIONING OF THIS SCRIPT.
  89. #
  90.  
  91. TODATE=$(date +%d)
  92. TOMORROW=`date +%d -d "1 day"`
  93. TODAY=$(date +%a)
  94. MONTH=$(date +%B)
  95. WEEK=$(date +%U)
  96.  
  97. if [ $TODATE -gt $TOMORROW ] && [ "$MONTHLYBACKUP" == "Y" ]
  98. then
  99. /usr/bin/mysqldump -h $DBHOSTNAME -u $DBUSERNAME -p$DBPASSWORD $DBNAME | gzip > $BACKUPFOLDER/$DBNAME'_'`date '+%m-%d-%Y'`'_'$MONTH.sql.gz
  100. else
  101. if [ "$TODAY" == "Sat" ] && [ "$WEEKLYBACKUP" == "Y" ]
  102. then
  103. /usr/bin/mysqldump -h $DBHOSTNAME -u $DBUSERNAME -p$DBPASSWORD $DBNAME | gzip > $BACKUPFOLDER/$DBNAME'_'`date '+%m-%d-%Y'`'_'Week$WEEK.sql.gz
  104. else
  105. if [ "$DAILYBACKUP" == "Y" ]
  106. then
  107. /usr/bin/mysqldump -h $DBHOSTNAME -u $DBUSERNAME -p$DBPASSWORD $DBNAME | gzip > $BACKUPFOLDER/$DBNAME'_'`date '+%m-%d-%Y'`'_'$TODAY.sql.gz
  108. fi
  109. fi
  110. fi
  111.  
  112.  
  113. if [ "$DELETEFILES" == "Y" ]
  114. then
  115. NUMWEEKLY=$[$NUMWEEKLYBACKUPS*7]
  116. NUMMONTHLY=$[$NUMMONTHLYBACKUPS*31]
  117. find $BACKUPFOLDER/*Sun.sql.gz -type f -mtime +$NUMDAILYBACKUPS -delete 2> /dev/null
  118. find $BACKUPFOLDER/*Mon.sql.gz -type f -mtime +$NUMDAILYBACKUPS -delete 2> /dev/null
  119. find $BACKUPFOLDER/*Tue.sql.gz -type f -mtime +$NUMDAILYBACKUPS -delete 2> /dev/null
  120. find $BACKUPFOLDER/*Wed.sql.gz -type f -mtime +$NUMDAILYBACKUPS -delete 2> /dev/null
  121. find $BACKUPFOLDER/*Thu.sql.gz -type f -mtime +$NUMDAILYBACKUPS -delete 2> /dev/null
  122. find $BACKUPFOLDER/*Fri.sql.gz -type f -mtime +$NUMDAILYBACKUPS -delete 2> /dev/null
  123. find $BACKUPFOLDER/*Sat.sql.gz -type f -mtime +$NUMDAILYBACKUPS -delete 2> /dev/null
  124. find $BACKUPFOLDER/*Week*.sql.gz -type f -mtime +$NUMWEEKLY -delete 2> /dev/null
  125. find $BACKUPFOLDER/*January.sql.gz -type f -mtime +$NUMMONTHLY -delete 2> /dev/null
  126. find $BACKUPFOLDER/*February.sql.gz -type f -mtime +$NUMMONTHLY -delete 2> /dev/null
  127. find $BACKUPFOLDER/*March.sql.gz -type f -mtime +$NUMMONTHLY -delete 2> /dev/null
  128. find $BACKUPFOLDER/*April.sql.gz -type f -mtime +$NUMMONTHLY -delete 2> /dev/null
  129. find $BACKUPFOLDER/*May.sql.gz -type f -mtime +$NUMMONTHLY -delete 2> /dev/null
  130. find $BACKUPFOLDER/*June.sql.gz -type f -mtime +$NUMMONTHLY -delete 2> /dev/null
  131. find $BACKUPFOLDER/*July.sql.gz -type f -mtime +$NUMMONTHLY -delete 2> /dev/null
  132. find $BACKUPFOLDER/*August.sql.gz -type f -mtime +$NUMMONTHLY -delete 2> /dev/null
  133. find $BACKUPFOLDER/*September.sql.gz -type f -mtime +$NUMMONTHLY -delete 2> /dev/null
  134. find $BACKUPFOLDER/*October.sql.gz -type f -mtime +$NUMMONTHLY -delete 2> /dev/null
  135. find $BACKUPFOLDER/*November.sql.gz -type f -mtime +$NUMMONTHLY -delete 2> /dev/null
  136. find $BACKUPFOLDER/*December.sql.gz -type f -mtime +$NUMMONTHLY -delete 2> /dev/null
  137. fi
Add Comment
Please, Sign In to add comment