Skip to content

Instantly share code, notes, and snippets.

@hellokaton
Created April 24, 2018 06:49
Show Gist options
  • Save hellokaton/35b57a69e304cb1dfc1a1dbc678ea6c6 to your computer and use it in GitHub Desktop.
Save hellokaton/35b57a69e304cb1dfc1a1dbc678ea6c6 to your computer and use it in GitHub Desktop.
在 Linux 上定时备份 MySQL 数据库,并发送备份到邮件
#!/bin/sh
#this is the prefix before the filename and can be anything you want
fileprefix='mysql_backup_';
#this is your mysql user - best to create a new mysql user called backup that has access to all databases
myuser='backup';
#your mysql password
mypass='s0mething$ecure123';
#the backup directory that you should put at the root, not public_html
#chmod 644 the backup folder and leave the trailing slash
backupdir='/home/{typically your main cpanel account here without brackets}/backup/';
#more emails can be added by using a , to separate
emailto='test@test.com';
#subject of the email
emailsubject='mysql backup';
#body of the email
emailbody='mysql db backup attached';
#shouldn't need to change below this
date=`date '+%Y-%m-%d'`;
file=$fileprefix$date.gz;
mysqldump -u$myuser -p$mypass --all-databases | gzip > $backupdir$file;
find $backupdir -name "$fileprefix*" -mtime +7 -type f -exec rm -rf {} ;
echo $emailbody | mutt -s "$emailsubject" -a $backupdir$file "$emailto";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment