Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.58 KB | None | 0 0
  1. #!/bin/bash
  2. # Simple backup-script written by Alexander Rydekull
  3.  
  4. # Some configurable variables
  5. PROJECTNAME=project
  6. USERNAME=<username>
  7. PASSWORD=<password>
  8.  
  9. DBFILENAME=${PROJECTNAME}-databases.sql
  10.  
  11. LOGDIR=/opt/mysqlbackup
  12. LOGFILE=${PROJECTNAME}-backup-logfile.txt
  13.  
  14. CLEANOLDBACKUPS=yes
  15. REMOVEBACKUPOLDERTHENTHISMANYDAYS=7
  16.  
  17. # Some script variables
  18. YESTERDAYSDATE=$(date "+%Y%m%d")
  19.  
  20. # Functions
  21. printlogmsg () {
  22.   echo "$(date "+%Y%m%d %H:%M:%S:") $1" >> ${LOGFILE}
  23.   echo "$(date "+%Y%m%d %H:%M:%S:") $1"
  24. }
  25.  
  26. # If you have a file backupped already, make sure you save it
  27. if [ -f "${DBFILENAME}" ]
  28. then
  29.   if [ -f "${YESTERDAYSDATE}-${DBFILENAME}" ]
  30.   then
  31.     printlogmsg "Err, yesterdays backup already exists, something is iffy, exiting."
  32.     exit 1
  33.   else
  34.     printlogmsg "Moving away yesterdays backup from ${DBFILENAME} to ${YESTERDAYSDATE}-${DBFILENAME}"
  35.     mv ${DBFILENAME} ${YESTERDAYSDATE}-${DBFILENAME}
  36.   fi
  37. fi
  38.  
  39. # Make a full backup of the databases
  40. printlogmsg "Full backup of database"
  41. mysqldump -u ${USERNAME} -p ${PASSWORD} --all-databases --single-transaction > ${DBFILENAME}
  42. printlogmsg "Full backup of database - done"
  43.  
  44. printlogmsg "gzip:ing old backup to reduce size"
  45. gzip ${YESTERDAYSDATE}-${DBFILENAME}
  46. printlogmsg "gzip:ing old backup to reduce size - done"
  47.  
  48. # Clean old backups
  49. printlogmsg "Cleaning old backups older then ${REMOVEBACKUPOLDERTHENTHISMANYDAYS} days"
  50. find ${LOGDIR} -name "*.gz" -mtime +${REMOVEBACKUPOLDERTHENTHISMANYDAYS} -exec rm {} \;
  51. printlogmsg "Cleaning old backups older then ${REMOVEBACKUPOLDERTHENTHISMANYDAYS} days - done"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement