Back up mysql databases using shell scripts

Source: Internet
Author: User

Shell script to back up the mysql database needs to close up some script programs in the near future. We can share with you a script for backing up a mysql database. based on the idea that the environment is ubuntu10.04server www.2cto.com, we can get the names of all the databases on the mysql server, filter out databases that do not need to be backed up. Use mysqldump to export SQL files of all databases cyclically. Use zip encryption to compress all SQL files and regularly clean up data. shell code: Database Export Code [html] #! /Bin/bash #1. database Information definition www.2cto.com mysql_host = "192.168.1.1" mysql_user = "root" mysql_passwd = "root" # SQL backup Directory root_dir = "/backup" back_dir = "/backup/databases" data_dir =" databases "store_dir =" database "if [! -D $ back_dir]; then mkdir-p $ back_dir fi # backup database array db_arr = $ (echo "show databases; "| mysql-u $ mysql_user-p $ mysql_passwd-h $ mysql_host) # nodeldb = "test1" # current date = $ (date-d '+ 0 days' + % Y % m % d) # zip package password zippasswd = "passwd" zipname = "lczh _" $ date ". zip "#2. go to the backup directory cd $ back_dir #3. cyclic backup for dbname in $ {db_arr} do if [$ dbname! = $ Nodeldb]; then sqlfile = $ dbname-$ date ". SQL "mysqldump-u $ mysql_user-p $ mysql_passwd-h $ mysql_host $ dbname> $ sqlfile fi done container 4.tar package all SQL files tar-zcPpf $ root_dir/$ store_dir/$ zipname -- directory/$ root_dir/$ data_dir # Delete the SQL file if [$? = 0]; then rm-r $ data_dir fi data regular cleanup Script Function regular cleanup the shell code of the backup file 14 days ago [html] #! /Bin/bash-#1. parameter configuration # mysql file backup directory backup_dir1 = "/backup/test1/" backup_dir2 = "/backup/test2/" backdir_arr = ($ backup_dir1 $ backup_dir2) # expiration time of the file www.2cto.com keep_time = 14 # in the current week, crontab executes week =$ (date + % W) in the week 7 of the odd number) flag = 'expr $ week % 2' #2. clear expired files and only execute if [$ flag-eq 1]; then for dir in $ {backdir_arr [*]} do if [-d $ dir] on the 7th day of an odd number of days. then # search for file data other than 14 days clean_arr = 'Find $ dir-type f-mtime + $ keep_time-exec ls {} \; 'For cleanfile in $ {clean_arr} do rm $ cleanfile done fi crontab configuration [html] 0 5 ** 7. After executing the cleanup script, remember www.2cto.com. You have better database backup methods or instructions. insufficient shell scripts, you can leave a message with the post. I promise to reply and cheer up!

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.