Skip to content

Instantly share code, notes, and snippets.

@fmtarif
Created December 27, 2014 14:01
Show Gist options
  • Select an option

  • Save fmtarif/7ae3d743cfe7576c0a44 to your computer and use it in GitHub Desktop.

Select an option

Save fmtarif/7ae3d743cfe7576c0a44 to your computer and use it in GitHub Desktop.
#linux #sysadmin #bash - DB backup script with log messages
#!/bin/sh
# from http://unix.stackexchange.com/a/125727
now="$(date +'%d_%m_%Y_%H_%M_%S')"
filename="db_backup_$now".gz
backupfolder="/var/www/vhosts/example.com/httpdocs/backups"
fullpathbackupfile="$backupfolder/$filename"
logfile="$backupfolder/"backup_log_"$(date +'%Y_%m')".txt
echo "mysqldump started at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"
mysqldump --user=mydbuser--password=mypass --default-character-set=utf8 mydatabase | gzip > "$fullpathbackupfile"
echo "mysqldump finished at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"
chown myuser "$fullpathbackupfile"
chown myuser "$logfile"
echo "file permission changed" >> "$logfile"
find "$backupfolder" -name db_backup_* -mtime +8 -exec rm {} \;
echo "old files deleted" >> "$logfile"
echo "operation finished at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"
echo "*****************" >> "$logfile"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment