MySQL regular backup database shell script detailed

Source: Internet
Author: User
Tags vars

1. vi/home/zhangy/database_bak.sh
2.
3. #!/bin/sh
4. # File:/home/zhangy/database_bak.sh
5. # Database Info bakupmysql TANK 2009/11/04
6. Db_user= "Root" #用户名
7. db_pass= "********" # Password
8. Db_name= "MyBlog" # Name of the data to be backed up
9.
# others VARs
Date= ' Date +%y_%m_%d ' # Date to back up
Yesterday= ' date-d yesterday +%y_%m_%d ' # Delete yesterday's backup
13.
Bin_dir= "/usr/local/mysql/bin"
Bck_dir= "/home/zhangy/database_bak" # Backup path
16.
CD $BCK _dir
18.
19. # Delete Previous backup of this database, because my Linux below also have 2G hard disk space, depressed.
If [f $YESTERDAY $db_name ". sql"]
Then.
Rm-f $YESTERDAY $db_name ". sql"
Fi.
24.
25. # Backup
${bin_dir}/mysqldump--opt-u${db_user}-p${db_pass} ${db_name} > ${bck_dir}/${date}${db_name}.sql
vi/home/zhangy/database_bak.sh
#!/bin/sh
# File:/home/zhangy/database_bak.sh
# Database Info bakupmysql TANK 2009/11/04
db_user= "Root" #用户名
Db_pass= "********" # Password
Db_name= "MyBlog" # Name of the data to be backed up
# others VARs
Date= ' Date +%y_%m_%d ' # Date to back up
Yesterday= ' date-d yesterday +%y_%m_%d ' # Delete yesterday's backup
Bin_dir= "/usr/local/mysql/bin"
Bck_dir= "/home/zhangy/database_bak" # Backup path
CD $BCK _dir
# Delete Previous backup of this database, because my Linux also has 2G hard disk space, depressed.
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

Two, regular execution
put the shell inside the crontab.
# to see if Crond started
[root@blackghost cron]# ps-e|grep crond
21519? 00:00:00 crond
# open crontab
[root@blackg Host cron]# crontab-e
# Add a line inside
* */home/zhangy/database_bak.sh
# Check to see if added
[Root@blackghost cron] # crontab-l
#
# don't EDIT this FILE manually!! Use Crontab-e INSTEAD.
#
#
* * */usr/sbin/run-cron/etc/cron.hourly
* */usr/sbin/run-cron/etc/cron.daily
22 * * 0/usr/sbin/run-cron/etc/cron.weekly
1 * */usr/sbin/run-cron/etc/cron.monthly
* */home/zh angy/www/bb.php
* */sbin/shutdown-h now
* */home/zhangy/database_bak.sh
Then exit

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.