Server Data Backup (shell timed script backup mysql)

Source: Internet
Author: User
Tags mysql backup
Mysql backup policies can be divided into four types:

1) directly copy database files (not recommended)

2) use mysqlhotcopy to back up the database

3) Use mysqldump to back up the database

4) use the master-slave replication mechanism (for real-time database backup)

Mysqldump is widely used and easy to operate. The following describes the mysqldump backup policy.

Data backup is divided into three steps:

1) regularly export the backup database and package it into a gz compressed package (Linux shell script +
Crontab)

MysqlFullBackup. sh

#! /Bin/sh

# Use mysqldump -- help get more detail.

#
# Define variables. Modify the variables as needed
# Define the script directory
ScriptsDir = 'pwd'

# Define the username and password used to back up the database
User = root
User Pwd = 123456

# Define the backup database name
DbNames = (database1 database2 database3)

# Define the Backup Directory
DataBackupDir =/home/zhanghuihua/backup

# Define the mail body File
EMailFile = $ dataBackupDir/log/email.txt

# Define email addresses
EMail = Huihua.Zhang@quidos.co.uk

# Define backup log files
LogFile = $ dataBackupDir/log/mysqlbackup. log

# DATE = 'date-I'
DATE = 'date-d "now" + % Y % m % d'

Echo 'date-d "now" + % Y-% m-% d % H: % M: % S "'> $ eMailFile

For dbName in $ {dbNames [*]}
Do
# Define the backup file name
DumpFile = $ dataBackupDir/db/mongodbname-mongodate. SQL .gz

# Use mysqldump to back up the database. set parameters as needed
Mysqldump-u $ user-p $ userPWD $ dbName | gzip> $ dumpFile
Done

If [[$? = 0]; then
Echo "DataBase Backup Success! ">>$ EMailFile
Else
Echo "DataBase Backup Fail! ">>$ EmailFile
Fi

# Write a log file
Echo "============================================" >>> logFile
Cat $ eMailFile> $ logFile
Echo $ dumpFile> $ logFile

# Send email notification
# Cat $ eMailFile | mail-s "MySQL Backup" $ eMail

2) regularly download to the local device through the ftp service (Linux
Shell script
+ Crontab)

Or back up the database from the server to the ftp server (Linux
Shell script
+ Crontab)

FtpDownload. sh

#! /Bin/bash
# Defining variables
DATE = 'date-d "now" + % m % d % y'

FtpServer = 192.168.1.103
FtpUser = zhanghuihua
FtpPasswd = 123456

RemoteDir =/backup/db
LocalDir =/home/zhanghuihua/backup

# Start ftp operations
/Usr/bin/ftp-niv <!
Open $ ftpServer
User $ ftpUser $ ftpPasswd
Binary
Cd $ remoteDir
LCD $ localDir

Mget database1-$DATE. SQL .gz
Mget database2-$DATE. SQL .gz
Mget database3-$DATE. SQL .gz
Bye
!

DbPutToFtp. sh

#! /Bin/bash
# Defining variables
DATE = 'date-d "now" + % Y % m % d'

FtpServer = 192.168.1.103
FtpUser = zhanghuihua
FtpPasswd = 123456

RemoteDir =/backup/db
LocalDir =/home/zhanghuihua/backup/db

# Start ftp operations
/Usr/bin/ftp-niv <!
Open $ ftpServer
User $ ftpUser $ ftpPasswd
Binary
Cd $ remoteDir
LCD $ localDir

Mput database1-$DATE. SQL .gz
Mput database2-$DATE. SQL .gz
Mput database3-$DATE. SQL .gz

Bye
!

3) regularly burn the disc (manual operation)

3) regularly burn the disc (manual operation)

Crontab scheduled task management

Crontab-e

01 03 ***/var/proftpd_data/backup/bin/mysqlFullBackup. sh

10 04 ***/var/proftpd_data/backup/bin/ftpDownload. sh

The integer value range and significance of the first five fields are as follows:

0 ~ 59
Indicates the score

1 ~ 23
Hours

1 ~ 31
Indicates the day

1 ~ 12
Month

0 ~ 6
Week (0 indicates Sunday)

Related Article

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.