一周循环完全备份
datestr=`date '+%Y%m%d'`;
weekday=`date '+%w'`;
folder='/home/mysql_backup';
printf "$datestr --> $weekday.sql\r\n" >> "$folder/backup.log"
mysqldump --opt --password=yourpass --all-databases > "$folder/$weekday.sql";
增量备份
转:http://blog.chinaunix.net/u/29134/showart_1194259.html
#!/bin/sh
#
# Created by david yeung at 2008-09-16
#
# File name:backup_increment
#
# Backup mysql's increment data after daily backup.
#
# Usage:
# If you database name is t_girl and you want to backup from 2008-09-16 08:13:26.
# ./backup_increment t_girl 20080916081326
#
# You can simple put this file into crontab in order to work automatically.
#
# Put your own database name here.
DBNAME=$1
# You own specific datatime.
# For example,2008-09-16 08:13:26 should be replaced with 20080916081326 here.
#
USERDATETIME=$2
# You own backup directory.
BACKUPDIR=/home/david_yeung/backup_db
# You own backup user.
# He must have lock tables,select,super privileges.
# Also replication client privileges if your mysqld was set up as Master/Slave.
USERNAME=backup_user
PASSWD=123456
# Socket path.
SOCKET=/tmp/mysql_3306.sock
# All the binary logs.
LOG=/usr/local/mysql/data/ytt-bin.[0-9]*
# Backup file name.
TARNAME="$BACKUPDIR"/incrementbackup"$1""$2"
# Copy all the binary logs here to prevent failed backup.
cp -rf `echo $LOG` /home/david_yeung/logs
# Change the current directory.
# If you have aready put mysql into your environment variable named path,you can comment this line and use simple mysql instead of /bin/mysql.
#
cd /usr/local/mysql
bin/mysql -u$USERNAME -p"$PASSWD" -S$SOCKET -e "purge master logs before ${USERDATETIME}"
bin/mysqlbinlog -u$USERNAME -p"$PASSWD" -S$SOCKET -d$DBNAME `echo $LOG` --start-datetime="$USERDATETIME"> "$TARNAME"
指定数据库完全备份
假设有三个库
m_site,m_bbs,m_cms.
#!/bin/sh
#
# MySQL Backup Scripts.
# Created by david.
#
# Created time:2008-01-09 11:10:49
#
USERNAME=backup_user
PASSWORD=123456
case "$1" in
m_site) BACKUP_FILE=/home/david_yeung/backup/backup_m_site`date '+%Y%m%d_%H%M%S'`.sql ;;
m_bbs) BACKUP_FILE=/home/david_yeung/backup/backup_m_bbs`date '+%Y%m%d_%H%M%S'`.sql ;;
m_cms) BACKUP_FILE=/home/david_yeung/backup/backup_m_cms`date '+%Y%m%d_%H%M%S'`.sql ;;
*) ;;
esac
# Start backup.
/usr/local/mysql/bin/mysqldump -u$USERNAME -p$PASSWORD -B $1 --add-drop-table -R > $BACKUP_FILE
# Compress backup file.
gzip $BACKUP_FILE
加入到CRONTAB里面去。
[david_yeung]# crontab -l
0 0 * * * /home/david_yeung/mysqlbackup_david m_site
0 0 * * * /home/david_yeung/mysqlbackup_david m_bbs
0 0 * * * /home/david_yeung/mysqlbackup_david m_cms
这个脚本来配合原来的备份脚本做善后工作,只保留最新三天内的备份。
文件名称示例:
[root@mysql131-3 backup]# ls -sihl
total 1.2G
11075627 4.0M -rw-r--r-- 1 root root 4.0M Jan 27 01:00 backup_bbs20080127_010001.sql.gz
14843905 4.0M -rw-r--r-- 1 root root 4.0M Jan 28 01:00 backup_bbs20080128_010001.sql.gz
11075601 4.0M -rw-r--r-- 1 root root 4.0M Jan 29 01:00 backup_bbs20080129_010001.sql.gz
11075629 44K -rw-r--r-- 1 root root 39K Jan 27 01:00 backup_cms20080127_010001.sql.gz
14843908 44K -rw-r--r-- 1 root root 39K Jan 28 01:00 backup_cms20080128_010001.sql.gz
11075608 44K -rw-r--r-- 1 root root 39K Jan 29 01:00 backup_cms20080129_010001.sql.gz
11075628 403M -rw-r--r-- 1 root root 403M Jan 27 01:00 backup_site20080127_010001.sql.gz
14843906 403M -rw-r--r-- 1 root root 403M Jan 28 01:00 backup_site20080128_010001.sql.gz
11075602 403M -rw-r--r-- 1 root root 403M Jan 29 01:00 backup_site20080129_010001.sql.gz
脚本内 容:
#!/bin/sh
#
# Created by david yeung 20080129.
#
# Delete overdue mysql backup file.
#
# Directory to list.
DIRNAME=/home/david_yeung/backup/
# Get the date part.
ARR1=`ls $DIRNAME | cut -d '_' -f3`
# Get three day ago.
CUR_DATE=`date +'%Y%m%d' -d '-3 day'`
# Delete the overdue file.
for CUR_FILENAME in $ARR1
do
RESULT=`echo $CUR_FILENAME | tr -d "[a-z]"`
if [ "$RESULT" -le "$CUR_DATE" ]
then
rm -rf `echo "${DIRNAME}backup_${CUR_FILENAME}_010001.sql.gz"`
fi
done
转于:http://itlife365.com/?post=44
分享到:
相关推荐
本文将详细介绍如何使用 `mysqldump` 工具进行全量备份和增量备份,以及如何结合shell脚本来实现定时备份。 首先,全量备份是备份数据库的所有数据,不依赖于之前的备份。在MySQL中,我们可以使用 `mysqldump` 命令...
- **增量备份**:增量备份只备份自上次全量或增量备份以来发生变化的数据。这可以节省存储空间,但恢复过程更复杂,可能需要多个备份文件。 2. **Shell脚本编程**: - **基础元素**:包括变量、条件语句(if-else...
MySQL作为广泛使用的开源关系型数据库管理系统,其数据备份通常采用多种策略,包括全量备份、增量备份和差异备份。针对MySQL 5.7.8及更高版本,我们可以利用shell脚本来实现自动化且可配置的多线程备份,从而提高...
3. **程序控制**:易语言提供了丰富的流程控制结构,如循环、条件判断等,用于实现复杂的备份逻辑,如定时备份、增量备份等。 4. **文件操作**:备份的数据库通常以SQL脚本或二进制文件形式保存到本地,因此需要...
9. **Crontab定时任务**:`crontab -e`允许用户编辑计划任务,脚本中的`0 3 * * * sh /home/backupDB.sh > /dev/null 2>&1 &`设置表示每天凌晨3点执行备份脚本,并将标准输出和错误输出重定向到`/dev/null`,避免...
`备份方案.txt`可能详细阐述了不同类型的备份方法,如增量备份、差异备份,并讨论它们的优缺点和适用场景。 **Linux Shell的程序编写:** `Linux Shell的程序编写.txt`和`Linux下的shell编程入门.txt`深入介绍了...
- 对于非常大的数据库,可以考虑增量备份和差异备份,以减少备份时间和存储空间。 - 记录并监控备份日志,以便及时发现和解决问题。 综上所述,MySQL全量备份是一个涉及数据库管理、文件操作和自动化任务调度的过程...
通过Cron定时任务定期执行备份脚本,确保数据安全。 **五、MySQL双向同步** MySQL的双向同步通常使用复制功能来实现,分为主-从复制和主-主复制。这里我们关注主-主复制,使得两个MySQL实例可以同时作为主节点,...
恢复措施与备份类型(如完全备份、增量备份)确实有关。 8. Word段落格式:Word中的段落可以根据设定的页边距自动调整行距,实现灵活的格式编排。 9. FTP下载:FTP(File Transfer Protocol)协议常用于文件的上传...
在备份和恢复方面,MySQL 5.6.49提供了更好的备份工具,如`mysqldump`和`mysqlpump`,它们可以快速生成数据库的完整或增量备份,方便在需要时进行恢复。 对于Windows 64位系统的用户,`mysql-5.6.49-winx64.zip`...