MYSQL code regularly backs up Mysql Databases

Source: Internet
Author: User
Tags mysql code mysql automatic backup

I believe that many webmasters or independent Blog bloggers have the need to back up databases. Of course, there are many plug-ins that can help you complete this task in a rich Blog system such as WordPress. If you need to back up the database by yourself, I will post a Mysql automatic backup script for your reference. What you can see on a foreign website is still good, so you can regularly back up the Mysql database, in addition, you can choose to make a full backup of files under the specified directory on the day of each week. The backup files are automatically uploaded to the specified FTP to ensure the reliability of backup. If you just back up the database together with the database, it doesn't mean much. Copy codeThe Code is as follows :#! /Bin/sh
# System + MySQL backup script
# Full backup day-Sun (rest of the day do incremental backup)
# Copyright (c) 2005-2006 nixCraft # This script is licensed under gnu gpl version 2.0 or above
# Automatically generated
# Http://bash.cyberciti.biz/backup/wizard-ftp-script.php
#---------------------------------------------------------------------
### System Setup ###
# Back up the local directory of the local file ##
DIRS = "/home/xxxx"
BACKUP =/tmp/backup. $
# Date formats can be adjusted by yourself ##
NOW = $ (date + "% Y-% m-% d ")
INCFILE = "/home/xxxx"
DAY = $ (date + "% u ")
# Daily full backup can be adjusted by yourself ##
FULLBACKUP = "7"
### MySQL Setup ###
MUSER = "chaos"
MPASS = "password"
MHOST = "host"
MYSQL = "$ (which mysql )"
MYSQLDUMP = "$ (which mysqldump )"
GZIP = "$ (which gzip )"
### FTP server Setup ###
# Incremental backup path ##
FTPD = "/backup/xxxx/incremental"
FTPU = "chaos"
FTPP = "password"
FTPS = "xxx.xxx.com"
### Other stuff ###
EMAILID = "chaos@diablo.net"
### Start Backup for file system ###
[! -D $ BACKUP] & mkdir-p $ BACKUP |:
### See if we want to make a full backup ###
If ["$ DAY" = "$ FULLBACKUP"]; then
# Full backup path ##
FTPD = "/backup/xxxx/full"
FILE = "fs-full-invalid now.tar.gz"
Tar-zcvf $ BACKUP/$ FILE $ DIRS
Else
I = $ (date + "% Hh % Mm % Ss ")
FILE = "fs-i-policnow-polici.tar.gz"
Tar-g $ INCFILE-zcvf $ BACKUP/$ FILE $ DIRS
Fi
### Start MySQL Backup ###
# Get all databases name
# You can modify or back up a specified database on your own to back up all databases ##
DBS = "$ ($ MYSQL-u $ MUSER-h $ MHOST-p $ MPASS-Bse 'show databases ')"
For db in $ DBS
Do
FILE = $ BACKUP/mysql-$ db. $ NOW-$ (date + "% T" 2.16.gz
$ MYSQLDUMP-u $ MUSER-h $ MHOST-p $ MPASS $ db | $ GZIP-9> $ FILE
Done
### Dump backup using FTP ###
# Start FTP backup using lftp
# Ncftp can be automatically adjusted on ubuntu ##
# Ncftp-u "$ FTPU"-p "$ FTPP" $ FTPS <EOF
Lftp-u $ FTPU, $ FTPP $ FTPS <EOF
Mkdir $ FTPD
Mkdir $ FTPD/$ NOW
Cd $ FTPD/$ NOW
LCD $ BACKUP
Mput *
Quit
EOF
### Find out if ftp backup failed or not ###
If ["$? "=" 0 "]; then
Rm-f $ BACKUP /*
Else
T =/tmp/backup. fail
Echo "Date: $ (date)"> $ T
Echo "Hostname: $ (hostname)" >>$ T
Echo "Backup failed" >>$ T
Mail-s "backup failed" "$ EMAILID" <$ T
Rm-f $ T
Fi

You can change the path, account, password, and other information to make it useful.

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.