Home Database Mysql Tutorial Linux下实现MySQL数据库每天自动备份定时备份

Linux下实现MySQL数据库每天自动备份定时备份

Jun 07, 2016 pm 02:58 PM
mysql backup

备份是容灾的基础,是指为防止系统出现操作失误或系统故障导致数据丢失,而将全部或部分数据集合从应用主机的硬盘或阵列复制到其

概述
备份是容灾的基础,是指为防止系统出现操作失误或系统故障导致数据丢失,而将全部或部分数据集合从应用主机的硬盘或阵列复制到其它的存储介质的过程。而对于一些网站、系统来说,,数据库就是一切,所以做好数据库的备份是至关重要的!

备份是什么?

Linux下实现MySQL数据库每天自动备份定时备份

为什么要备份

Linux下实现MySQL数据库每天自动备份定时备份

容灾方案建设

Linux下实现MySQL数据库每天自动备份定时备份

存储介质
光盘
磁带
硬盘
磁盘阵列
DAS:直接附加存储
NAS:网络附加存储
SAN:存储区域网络
云存储

这里主要以本地磁盘为存储介质讲一下计划任务的添加使用,基本的备份脚本,其它存储介质只是介质的访问方式可能不大一样。

1、查看磁盘空间情况:
既然是定时备份,就要选择一个空间充足的磁盘空间,避免出现因空间不足导致备份失败,数据丢失的恶果!
存储到当前磁盘这是最简单,却是最不推荐的;服务器有多块硬盘,最好是把备份存放到另一块硬盘上;有条件就选择更好更安全的存储介质;

# df -h
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root  50G  46G  1.6G  97% /
tmpfs                        1.9G  92K  1.9G  1% /dev/shm
/dev/sda1                    485M  39M  421M  9% /boot
/dev/mapper/VolGroup-lv_home  534G  3.6G  503G  1% /home

2、创建备份目录:
上面我们使用命令看出/home下空间比较充足,所以可以考虑在/home保存备份文件;

cd /home
mkdir backup
cd backup

3、创建备份Shell脚本:
注意把以下命令中的DatabaseName换为实际的数据库名称;
当然,你也可以使用其实的命名规则!

vi bkDatabaseName.sh

输入/粘贴以下内容:

#!/bin/bash
mysqldump -uusername -ppassword DatabaseName > /home/backup/DatabaseName_$(date +%Y%m%d_%H%M%S).sql

对备份进行压缩:

#!/bin/bash
mysqldump -uusername -ppassword DatabaseName | gzip > /home/backup/DatabaseName_$(date +%Y%m%d_%H%M%S).sql.gz

注意:
把 username 替换为实际的用户名;
把 password 替换为实际的密码;
把 DatabaseName 替换为实际的数据库名;

4、添加可执行权限:
chmod u+x bkDatabaseName.sh
添加可执行权限之后先执行一下,看看脚本有没有错误,能不能正常使用;

./bkDatabaseName.sh

5、添加计划任务
检测或安装 crontab
确认crontab是否安装:
执行 crontab 命令如果报 command not found,就表明没有安装

# crontab
-bash: crontab: command not found1
如时没有安装 crontab,需要先安装它,具体步骤请参考:
CentOS下使用yum命令安装计划任务程序crontab
使用rpm命令从CentOS系统盘安装计划任务程序crontab

添加计划任务
执行命令:

crontab -e

这时就像使用vi编辑器一样,可以对计划任务进行编辑。
输入以下内容并保存:

*/1 * * * * /home/backup/bkDatabaseName.sh

具体是什么意思呢?
意思是每一分钟执行一次shell脚本“/home/backup/bkDatabaseName.sh”。

6、测试任务是否执行
很简单,我们就执行几次“ls”命令,看看一分钟过后文件有没有被创建就可以了!

如果任务执行失败了,可以通过以下命令查看任务日志:

# tail -f /var/log/cron

输出类似如下:

Sep 30 14:01:01 bogon run-parts(/etc/cron.hourly)[2503]: starting 0anacron
Sep 30 14:01:01 bogon run-parts(/etc/cron.hourly)[2512]: finished 0anacron
Sep 30 15:01:01 bogon CROND[3092]: (root) CMD (run-parts /etc/cron.hourly)
Sep 30 15:01:01 bogon run-parts(/etc/cron.hourly)[3092]: starting 0anacron
Sep 30 15:01:02 bogon run-parts(/etc/cron.hourly)[3101]: finished 0anacron
Sep 30 15:50:44 bogon crontab[3598]: (root) BEGIN EDIT (root)
Sep 30 16:01:01 bogon CROND[3705]: (root) CMD (run-parts /etc/cron.hourly)
Sep 30 16:01:01 bogon run-parts(/etc/cron.hourly)[3705]: starting 0anacron
Sep 30 16:01:01 bogon run-parts(/etc/cron.hourly)[3714]: finished 0anacron
Sep 30 16:15:29 bogon crontab[3598]: (root) END EDIT (root)

本文永久更新链接地址

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Multiple backup solutions for MySql: How to efficiently create and restore MySQL backups Multiple backup solutions for MySql: How to efficiently create and restore MySQL backups Jun 15, 2023 pm 03:28 PM

MySql is a commonly used relational database management system that is widely used in various business and application scenarios. For MySQL backup issues, the selection and execution method of the backup plan are crucial. In this article, we will introduce various backup options and how to create and restore MySQL backups efficiently. 1. Selection of backup plan In the process of selecting a MySQL backup plan, you should choose a backup plan that suits you based on the business scenario and actual situation. Cold backup The so-called cold backup is to complete the MySQL database.

How to effectively manage and maintain ibd files in MySQL database How to effectively manage and maintain ibd files in MySQL database Mar 16, 2024 am 11:21 AM

In the MySQL database, each InnoDB table corresponds to an .ibd file, which stores the table's data and indexes. Therefore, for the management and maintenance of MySQL database, the management of ibd files is also particularly important. This article will introduce how to effectively manage and maintain ibd files in a MySQL database and provide specific code examples. 1. Check and optimize table space First, we can check the disk space usage of the table using the following SQL statement: SELECTTAB

How to use MySQL data backup and recovery tools for disaster recovery How to use MySQL data backup and recovery tools for disaster recovery Aug 02, 2023 am 09:06 AM

How to use MySQL data backup and recovery tools to achieve disaster recovery. Data backup and recovery are a very important part of the database management process. Backing up your data protects your database from accidental corruption, hardware failure, or other catastrophic events. As a popular relational database management system, MySQL provides some powerful tools to achieve data backup and recovery. This article will introduce how to use MySQL's data backup and recovery tools to achieve disaster recovery. MySQL data backup tool-mysql

MySQL rolling backup techniques for data MySQL rolling backup techniques for data Jun 15, 2023 pm 07:47 PM

MySQL is a popular relational database that is widely used in various fields. However, like other applications, MySQL has risks such as data corruption, crashes, and malicious attacks. Therefore, backing up your data is crucial. Backups can provide security and some form of "undo" functionality to data, reducing or even eliminating instability and risk. The most common backup types are full backup and incremental backup. However, if you need frequent, real-time backups, rolling backups are a better approach. A rolling backup is when an acceptable

MySql database backup: How to achieve efficient MySQL database backup and recovery MySql database backup: How to achieve efficient MySQL database backup and recovery Jun 15, 2023 pm 11:37 PM

MySQL is one of the most widely used relational database management systems currently. Its efficiency and reliability make it the first choice for many enterprises and developers. But for various reasons, we need to back up the MySQL database. Backing up a MySQL database is not an easy task because once the backup fails, important data may be lost. Therefore, in order to ensure data integrity and recoverability, some measures must be taken to achieve efficient MySQL database backup and recovery. This article will introduce how to achieve

Analysis of project experience on MySQL database backup and recovery performance tuning Analysis of project experience on MySQL database backup and recovery performance tuning Nov 04, 2023 am 09:46 AM

Analysis of MySQL database backup and recovery performance tuning project experience In daily operation and maintenance, MySQL database backup and recovery work is indispensable. However, in the face of multi-terabyte or even petabyte-level data scale, the time and resource consumption required for backup and recovery often become key factors restricting database performance. This article will share some practical experiences and techniques through a practical case of backup and recovery performance tuning for a large Internet enterprise. 1. Selection of backup solutions. Based on different business needs and data scale, the selection of backup solutions should also be considered.

Project experience summary of MySQL database backup and disaster recovery solutions Project experience summary of MySQL database backup and disaster recovery solutions Nov 02, 2023 pm 03:54 PM

Project experience summary of MySQL database backup and disaster recovery plan In the project, the database backup and disaster recovery plan is a very important work content. As a commonly used relational database management system, MySQL’s formulation and implementation of backup and disaster recovery plans are key to ensuring data security and availability. I have accumulated some experience in past project implementations and summarized them as follows. 1. Formulation of backup strategy Developing a reasonable backup strategy is crucial to the security of the database. Generally speaking, backup strategies need to consider the following:

A comprehensive guide to MySQL backup and recovery A comprehensive guide to MySQL backup and recovery Jun 15, 2023 am 09:48 AM

MySQL is currently one of the most popular relational database management systems and is widely used in enterprise-level applications. Whether you are a developer or a data administrator, you need to understand the basic knowledge of MySQL backup and recovery. Backup and recovery not only help enterprises protect data, but also enable systems to respond quickly to adverse situations and restore them to normal operating conditions as much as possible. This article will detail the steps for MySQL backup and recovery and provide some best practices to help readers go further in protecting their MySQL databases.

See all articles