您的位置:首页 > 数据库 > MySQL

ubantu下MySQL数据库备份方法

2015-09-07 09:58 531 查看
1、编辑/etc/crontab文件设定定时任务,在制定时间执行backup_databases.sh

vi /etc/crontab

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

25 9 * * *  root sh /home/autotest/backup_databases.sh


2、编写backup_databases.sh,内容如下:

#!/bin/bash
# Set the backup filename and directory
DATE=`date +%Y%m%d` # e.g 20101025
FILENAME="trunk_development_database_$DATE.sql";
BACKUPDIR="/backup/";
# Database Credentials
DBUSER="root";
DBPWD="123qwe^&*";
DBNAME="trunk_development";
# Change to the root directory
cd /
# Where is our gzip tool for compression?
# The -f parameter will make sure that gzip will
# overwrite existing files
GZIP="/bin/gzip -f";
# Delete old backups older than 3 days
find /backup/trunk_development_*gz -mtime +3 -exec rm {} \;
# execute the database dump
mysqldump --user=$DBUSER --password=$DBPWD --add-drop-table --databases $DBNAME > $BACKUPDIR$FILENAME
# compress the database backup$GZIP $BACKUPDIR$FILENAME


3、手动创建目录/backup
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: