Script code for mysql regular backup in linux

Source: Internet
Author: User

I. Prepare shell scripts


Vi/home/zhangy/database_bak.sh

#! /Bin/sh
# File:/home/zhangy/database_bak.sh
# Database info bakupmysql TANK 2009/11/04
DB_USER = "root" # User Name
DB_PASS = "*********" # Password
DB_NAME = "myblog" # Name of the data to be backed up

<SPAN id = more-161> </SPAN> # Others vars
DATE = 'date + % Y _ % m _ % d' # date to be backed up
YESTERDAY = 'date-d yesterday + % Y _ % m _ % d' # Delete the backup from YESTERDAY

BIN_DIR = "/usr/local/mysql/bin"
BCK_DIR = "/home/zhangy/database_bak" # backup path

Cd $ BCK_DIR

# Delete the backup of the database in the past because I still have 2 GB hard disk space in linux, Which is depressing.
If [-f $ YESTERDAY $ DB_NAME ". SQL"]
Then
Rm-f $ YESTERDAY $ DB_NAME ". SQL"
Fi

# Backup
$ {BIN_DIR}/mysqldump -- opt-u $ {DB_USER}-p $ {DB_PASS }$ {DB_NAME }>$ {BCK_DIR}/$ {DATE }$ {DB_NAME}. SQL

Ii. Regular execution
Put shell in crontab.
# Check whether crond is started
[Root @ BlackGhost cron] # ps-e | grep crond
21519? 00:00:00 crond
# Open crontab
[Root @ BlackGhost cron] # crontab-e
# Add a line in it
00 18 ***/home/zhangy/database_bak.sh
# Check if any added

[Root @ BlackGhost cron] # crontab-l
#
# Do not edit this file manually !! USE crontab-e INSTEAD.
#
# <Minute> 01 ***/usr/sbin/run-cron/etc/cron. hourly
02 00 ***/usr/sbin/run-cron/etc/cron. daily
22 00 ** 0/usr/sbin/run-cron/etc/cron. weekly
42 00 1 **/usr/sbin/run-cron/etc/cron. monthly
56 06 ***/home/zhangy/www/bb. php
51 23 ***/sbin/shutdown-h now
00 18 ***/home/zhangy/database_bak.sh

Then exit
 

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.