Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Simple backup-script written by Alexander Rydekull
- # Some configurable variables
- PROJECTNAME=project
- USERNAME=<username>
- PASSWORD=<password>
- DBFILENAME=${PROJECTNAME}-databases.sql
- LOGDIR=/opt/mysqlbackup
- LOGFILE=${PROJECTNAME}-backup-logfile.txt
- CLEANOLDBACKUPS=yes
- REMOVEBACKUPOLDERTHENTHISMANYDAYS=7
- # Some script variables
- YESTERDAYSDATE=$(date "+%Y%m%d")
- # Functions
- printlogmsg () {
- echo "$(date "+%Y%m%d %H:%M:%S:") $1" >> ${LOGFILE}
- echo "$(date "+%Y%m%d %H:%M:%S:") $1"
- }
- # If you have a file backupped already, make sure you save it
- if [ -f "${DBFILENAME}" ]
- then
- if [ -f "${YESTERDAYSDATE}-${DBFILENAME}" ]
- then
- printlogmsg "Err, yesterdays backup already exists, something is iffy, exiting."
- exit 1
- else
- printlogmsg "Moving away yesterdays backup from ${DBFILENAME} to ${YESTERDAYSDATE}-${DBFILENAME}"
- mv ${DBFILENAME} ${YESTERDAYSDATE}-${DBFILENAME}
- fi
- fi
- # Make a full backup of the databases
- printlogmsg "Full backup of database"
- mysqldump -u ${USERNAME} -p ${PASSWORD} --all-databases --single-transaction > ${DBFILENAME}
- printlogmsg "Full backup of database - done"
- printlogmsg "gzip:ing old backup to reduce size"
- gzip ${YESTERDAYSDATE}-${DBFILENAME}
- printlogmsg "gzip:ing old backup to reduce size - done"
- # Clean old backups
- printlogmsg "Cleaning old backups older then ${REMOVEBACKUPOLDERTHENTHISMANYDAYS} days"
- find ${LOGDIR} -name "*.gz" -mtime +${REMOVEBACKUPOLDERTHENTHISMANYDAYS} -exec rm {} \;
- printlogmsg "Cleaning old backups older then ${REMOVEBACKUPOLDERTHENTHISMANYDAYS} days - done"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement