Linux timed backup MySQL

Source: Internet
Author: User

Scheduled backups:
1. Create a backup file to store the directory
2. Create a backup script backup.sh

#!/bin/bash

Dbuser=db_back
Dbpasswd=password
Dbname=db_name
Server=localhost
save_dir=/dbbackup/
logfile= "$save _dir" Backup.log
newfile= "$save _dir" $DBName ' _ ' $ (date +%y%m%d). sql
oldfile= "$save _dir" $DBName ' _ ' $ (date +%y%m%d--date= ' days ago '). sql


If [-F $OldFile]
Then
Rm-f $OldFile >> $LogFile 2>&1
echo "[$OldFile]delete old File success!" >> $LogFile
Else
echo "[$OldFile]no old Backup file! >> $LogFile
Fi

If [-F $NewFile]
Then
echo "[$NewFile]the Backup File is Exists,can ' t backup!" >> $LogFile
echo "Begin ro recover the data ..."
echo "Data has been restored!" >> $LogFile
Else
mysqldump-h$server-u$dbuser-p$dbpasswd--default-character-set= ' UTF8 '--quick--single-transaction-r $DBName >$ NewFile
echo "[$NewFile]backup success!" >> $LogFile
Fi


3. Make script executable
4, modify the/etc/crontab

Crontab-e

* * * */absolute/path/to/backup.sh daily 22-point backup
5. Restart Crond
/etc/rc.d/init.d/crond restart
View the cron configuration for the current user, using Crontab-l
Edit the cron configuration for the current user, using CRONTAB-E
Delete the cron configuration for the current user, using Crontab-r
View/edit/delete a user's cron configuration as root, add-u USERNAME after the command
Configure system-level tasks, edit/etc/crontab files

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.