Home Database Mysql Tutorial linux下使用crontab定时备份MYSQL数据库的方法:_MySQL

linux下使用crontab定时备份MYSQL数据库的方法:_MySQL

Jun 01, 2016 pm 01:10 PM

只需按照下面3步做,一切都在你的掌控之下: 

第一步:在服务器上配置备份目录代码: 

-------------------------------------------------------------------------------- 

mkdir /var/lib/mysqlbackup 

cd /var/lib/mysqlbackup 

-------------------------------------------------------------------------------- 

第二步:编写备份脚本代码: 

-------------------------------------------------------------------------------- 

vi dbbackup.sh 

-------------------------------------------------------------------------------- 

粘帖以下代码,务必更改其中的username,password和dbname。 

代码: 

-------------------------------------------------------------------------------- 

#!/bin/sh

mysqldump -uuser -ppassword dbname | gzip > /var/lib/mysqlbackup/dbname`date +%Y-%m-%d_%H%M%S`.sql.gz

cd  /var/lib/mysqlbackup

   rm -rf `find . -name '*.sql.gz' -mtime 10`  #删除10天前的备份文件

-------------------------------------------------------------------------------- 

第三步:更改备份脚本权限 

代码: 

-------------------------------------------------------------------------------- 

chmod +x dbbackup.sh 

-------------------------------------------------------------------------------- 

第四步:用crontab定时执行备份脚本代码: 

-------------------------------------------------------------------------------- 

crontab -e 

-------------------------------------------------------------------------------- 

若每天晚上21点00备份,添加如下代码, 

代码: 

-------------------------------------------------------------------------------- 

00 21 * * * /var/lib/mysqlbackup/dbbackup.sh




注:

1、任务调度设置文件的写法
      可用crontab -e命令来编辑,编辑的是/var/spool/cron下对应用户的cron文件,也可以直接修改/etc/crontab文件
     具体格式如下:
      Minute Hour Day Month Dayofweek   command
      分钟     小时   天     月       天每星期       命令
     每个字段代表的含义如下:
     Minute             每个小时的第几分钟执行该任务
     Hour               每天的第几个小时执行该任务
     Day                 每月的第几天执行该任务
     Month             每年的第几个月执行该任务
     DayOfWeek     每周的第几天执行该任务
     Command       指定要执行的程序
     在这些字段里,除了“Command”是每次都必须指定的字段以外,其它字段皆为可选

    字段,可视需要决定。对于不指定的字段,要用“*”来填补其位置。
    举例如下:
    5       *       *           *     *     ls             指定每小时的第5分钟执行一次ls命令
    30     5       *           *     *     ls             指定每天的 5:30 执行ls命令
    30     7       8         *     *     ls             指定每月8号的7:30分执行ls命令
    30     5       8         6     *     ls             指定每年的6月8日5:30执行ls命令
    30     6       *           *     0     ls             指定每星期日的6:30执行ls命令[注:0表示星期天,1表示星期1,

    以此类推,也可以用英文来表示,sun表示星期天,mon表示星期一等。]

   30     3     10,20     *     *     ls     每月10号及20号的3:30执行ls命令[注:“,”用来连接多个不连续的时段]

    25     8-11 *           *     *     ls       每天8-11点的第25分钟执行ls命令[注:“-”用来连接连续的时段]

    */15   *       *           *     *     ls         每15分钟执行一次ls命令 [即每个小时的第0 15 30 45 60分钟执行ls命令 ]

     30   6     */10         *     *     ls       每个月中,每隔10天6:30执行一次ls命令[即每月的1、11、21、31日是的6:30执行一次ls 命令。 ]

     每天7:50以root 身份执行/etc/cron.daily目录中的所有可执行文件

     50   7       *             *     *     root     run-parts     /etc/cron.daily   [ 注:run-parts参数表示,执行后面目录中的所有可执行文件。 ]

2、新增调度任务可用两种方法:
       1)、在命令行输入: crontab -e 然后添加相应的任务,wq存盘退出。
        2)、直接编辑/etc/crontab 文件,即vi /etc/crontab,添加相应的任务。

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

When might a full table scan be faster than using an index in MySQL? When might a full table scan be faster than using an index in MySQL? Apr 09, 2025 am 12:05 AM

Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

Explain InnoDB Full-Text Search capabilities. Explain InnoDB Full-Text Search capabilities. Apr 02, 2025 pm 06:09 PM

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

Can I install mysql on Windows 7 Can I install mysql on Windows 7 Apr 08, 2025 pm 03:21 PM

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

Difference between clustered index and non-clustered index (secondary index) in InnoDB. Difference between clustered index and non-clustered index (secondary index) in InnoDB. Apr 02, 2025 pm 06:25 PM

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values ​​and pointers to data rows, and is suitable for non-primary key column queries.

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

How do you handle large datasets in MySQL? How do you handle large datasets in MySQL? Mar 21, 2025 pm 12:15 PM

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

The relationship between mysql user and database The relationship between mysql user and database Apr 08, 2025 pm 07:15 PM

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

See all articles