Shell realizes automatic backup of MySQL, whole station data two scripts share _linux shell

Source: Internet
Author: User
Tags mysql automatic backup

Case one: Shell+cron realize MySQL automatic backup and automatically delete n days before backup

Copy Code code as follows:

#!/bin/sh
Dump=/usr/local/mysql/bin/mysqldump
out_dir=/home/ldl/xxx/backup/
Linux_user=ldl

Db_name=ldl
Db_user=ldl
Db_pass=xxx
#How much of the days of backup most
Days=1

#Core of script
CD $OUT _dir
Date= ' Date +%y_%m_%d '
Out_sql= "$DATE. SQL"
Tar_sql= "Mysql_$date.tar.gz"
$DUMP--default-character-set=utf8--opt-u$db_user-p$db_pass $DB _name > $OUT _sql
TAR-CZF $TAR _sql./$OUT _sql
RM $OUT _sql
Chown $DB _name:nobody./$TAR _sql
Find./-name "mysql*"-type f-mtime + $DAYS-exec rm {} \;


+++++++++++++++++++++++++++++++++++++++++++++++++++++
Mysqldump do not have to explain it, note-uuser-ppass This can be, cannot have spaces.

Copy Code code as follows:
Find./-name "mysql*"-type f-mtime + $DAYS-exec rm {} \;

This sentence is mainly on the-mtime-mtime +5 represents 5 days before the file, unified Delete

My operating system is CentOS 5.4, as long as the script copy to/etc/cron.daily, and set to the same permissions can be executed, no longer add crontab.

Case two: CentOS to automatically back up the Web site and database scripts and upload to FTP

Assume that the Web site directory for this server is:/HOME/WWW, the database program path is:/usr/local/mysql/bin, the database name is: LEVIL,FTP Server is: Ftphost, the following first to see the Complete automatic backup script (automatic backup script save location:/ home/backup.sh):

Copy Code code as follows:

#!/bin/bash
Cd/home
webbakname=web_$ (date +%y%m%d). tar.gz
Tar zcvf $WebBakName www
sqlbakname=sql_$ (date +%y%m%d). tar.gz
/usr/local/mysql/bin/mysqldump-uusername-ppassword Levil>backup.sql
Tar zcvf $SqlBakName backup.sql
FTP-NV ftphost << EOF
User Ftpname Ftppass
Put $WebBakName
Put $SqlBakName
Quit
Eof
Rm-f Backup.sql $WebBakName $SqlBakName

Here is a step-by-step explanation: First go to the/home directory, define the Webbakname variable for the site backup file name, The file name format is the Web date. tar.gz, which defines the file name of the Sqlbakname variable to be backed up by the database, and the filename format is the SQL date. tar.gz, the entire Web site Directory www package to webbakname filename, use mysqldump to export the specified database content to backup.sq L, and then package the database backup into the Sqlbakname file, this is the end of the local backup work, if you do not have large enough remote FTP space, you can directly download the backup file to this machine, but I suggest that the direct backup to another server in the FTP space, do fully automated backup, Then you need to replace the ftphost, Ftpname and Ftppass in the script with your FTP information, and the entire backup process is complete.

Then enter chmod +x backup.sh to add execution permissions to the script, and then enter: CRONTAB-E Edit task automatic start time, such as I enter:

1/home/backup.sh on behalf of the 1 5 o'clock in the morning per week to perform this automatic backup operation.

After all this has been done, you can change the automatic task time to the current approximate time, to see whether the automatic backup script is working properly, test OK then do not worry about this server any problems caused by the loss of data, of course, if your data update more frequently, we recommend that the automatic backup time to adjust to the daily

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.