Mysql automatic backup script code in linux _ MySQL

Source: Internet
Author: User
Tags mysql automatic backup mysql login
In linux, the mysql automatic backup script code bitsCN.com is stored in/home/user/mysql_backup.sh.
Crontab
# Crontab-l
# M h dom mon dow command
28 16 ***/home/user/mysql_backup.sh
The script is as follows:

#! /Bin/sh
# Mysql_backup.sh: backup mysql databases and keep newest 5 days backup.
#
# Last updated: 20 march2006
#----------------------------------------------------------------------
# This is a free shell script under gnu gpl version 2.0 or above
# Copyright (C) 2006 Sam Tang
# Feedback/comment/suggestions: http://www.real-blog.com/
#----------------------------------------------------------------------
# Your mysql login information
# Db_user is mysql username
# Db_passwd is mysql password
# Db_host is mysql host
#-----------------------------
Db_user = "root"
Db_passwd = "password"
Db_host = "localhost"
# The directory for story your backup file.
Backup_dir = "/home/mybackup"
# Date format for backup file (dd-mm-yyyy)
Time = "$ (date +" % d-% m-% Y ")"
# Mysql, mysqldump and some other bin's path
MYSQL = "/usr/local/mysql/bin/mysql"
MYSQLDUMP = "/usr/local/mysql/bin/mysqldump"
MKDIR = "/bin/mkdir"
RM = "/bin/rm"
MV = "/bin/mv"
GZIP = "/bin/gzip"
# Check the directory for store backup is writeable
Test! -W $ backup_dir & echo "Error: $ backup_dir is un-writeable." & exit 0
# The directory for story the newest backup
Test! -D "$ backup_dir/backup.0/" & $ MKDIR "$ backup_dir/backup.0 /"
# Get all databases
All_db = "$ ($ MYSQL-u $ db_user-h $ db_host-p $ db_passwd-Bse 'show databases ')"
For db in $ all_db
Do
$ MYSQLDUMP-u $ db_user-h $ db_host-p $ db_passwd $ db | $ GZIP-9> "$ backup_dir/backup.0/snapshot"
Done
# Delete the oldest backup
Test-d "$ backup_dir/backup.5/" & $ RM-rf "$ backup_dir/backup.5"
# Rotate backup directory
For int in 4 3 2 1 0
Do
If (test-d "$ backup_dir"/backup. "$ int ")
Then
Next_int = 'expr $ int + 1'
$ MV "$ backup_dir"/backup. "$ int" "$ backup_dir"/backup. "$ next_int"
Fi
Done
Exit 0;

Note:
Mysql runs as a mysql User and cannot be written to/home/mybackup.
Chmod 777/home/mybackup solved bitsCN.com

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.