Created
February 15, 2017 06:19
-
-
Save vzool/a75b0e7224d9eb22adaa50504d332149 to your computer and use it in GitHub Desktop.
Backup MySQL Database and keep history for 8 days
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| 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 | |
| # cd /etc/cron.daily/ | |
| # touch /etc/cron.daily/dbbackup-daily.sh | |
| # chmod 755 /etc/cron.daily/dbbackup-daily.sh | |
| # vi /etc/cron.daily/dbbackup-daily.sh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment