Linux regularly backs up MySQL database and deletes backup files up to seven days ago

Source: Internet
Author: User

1. Create a backup folder

#cd/bak
#mkdir Mysqldata

2. Write Run script

#nano-W/usr/sbin/bakmysql.sh

Note: If you use Nano to edit this code, add ' && ' or '; ' at the end of each line Connector, or the end character of the resulting file name is garbled

Code:

#!/bin/bash# Name:bakmysql.sh# This was a shellscript for Auto DB backup and Delete old Backup#Backupdir=/Bak/Mysqlbak Time=` Date+%Y%M%D%H`Mysql_bin_dir/Mysqldump- uUser-ppasswordDataname1| gzip > $backupdir/Name1$time. sql.gzmysql_bin_dir/Mysqldump- uUser-ppasswordDataname2| gzip > $backupdir/Name2$time. sql.gz#Find $backupdir -name "Name_*.sql.gz" -typeF-mtime+7 -exec RM {};> /Dev/Null2>&1
#
#
#
#

Save exit

Description

Time= ' Date +%y%m%d%h ' in the code can also be written as Time= "$ (date +"%y%m%d$h ")"
Where the ' symbol is the symbol above the TAB key, not the ' left ' sign of enter, and a space after date.
The bin path of the Mysql_bin_dir:mysql;
Dataname: Database name;
User: Database username;
Password: User password;
Name: custom backup file prefix identification.
-type f means finding a normal type of file, and F represents a normal file.
-mtime +7 The file changes time to find the file, +7 indicates that the file change time is 7 days ago, or if-mmin +7 means that the file change time is now 7 minutes ago.
-exec RM {}; Represents executing a shell command, followed by the EXEC option with the command or script to execute, followed by a pair of {}, a space and one, and finally a semicolon.
/dev/null 2>&1 redirects the standard error to standard output and throws it under/dev/null. In layman's words, all standard output and standard errors are thrown into the trash, where the & means that the command executes in the background.

3. Add Execute permissions for scripts

# chmod +x/usr/sbin/bakmysql.sh

4, modify the/etc/crontab (in the centOS5 test is feasible)

#nano-W/etc/crontab

In the last line, add:

XX 3 * * * root/usr/sbin/bakmysql.sh
Indicates that backups are performed 3:00 every day

Note: The crontab configuration file format is as follows:
Time-sharing Weekly command

Redhat Method:

Redhat's crontab takes 4 directories per time (/etc/cron.hourly: hourly;/etc/cron.daily: every day;/etc/cron.weekly: Weekly;/etc/ Cron.monthly: Monthly) The way the script comes out of the running. Just copy the script you just edited to the appropriate directory in the Redhat.

5. Restart Crontab

#/etc/rc.d/init.d/crond Restart

Complete.

6. Restore Data Backup files:

Non-compressed backup file recovery:

#mysql-u root-p dataname < Name2011070103.sql

To restore directly from a compressed file:

#gzip < name2011070103.sql.gz | Mysql-u root-p Dataname

Or:

# Zcat Name2011070103.sql.gz | Mysql-u root-p

Linux regularly backs up MySQL database and deletes backup files up to seven days ago

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.