Linux timed backup database

Source: Internet
Author: User

# You can put this script in/etc/crontab, 01 0 *** mysqlbackup. Sh
# Well, the system will automatically run the backup. Sh file to back up the MySQL database at every day.
# The automatic backup script can be executed only once a day, and only the backups of the last five days are retained on the server.
# Code:

#! /Bin/bash
# Setting
# Set the database name, database login name, password, backup path, Log Path, data file location, and backup mode
# By default, the backup method is mysqldump, mysqldump, or mysqldotcopy. If you comment out
# Line 18, which is backed up by TAG by default
# By default, log on to the MySQL database with root (empty) and back up to/root/dbxxxxx. tgz
Dbname = MySQL
Dbuser = root
Dbpasswd = 123456
Backuppath =/home/Wang/www/backup/MySQL/mysqlbackup
Logfile =/home/Wang/www/backup/MySQL/DB. Log
Dbpath =/var/lib/MySQL/
# Backupmethod = mysqldump
# Backupmethod = mysqlhotcopy
# Backupmethod = tar
# Setting end

Newfile = "$ backuppath" DB $ (date + % Y % m % d). tgz
Dumpfile = "$ backuppath" DB $ (date + % Y % m % d)
Oldfile = "$ backuppath" DB $ (date + % Y % m % d -- date = '5 days ago '). tgz
Echo "-----------------------------------------"> $ logfile
Echo $ (date + "% Y-% m-% d % H: % m: % s") >>$ logfile
Echo "--------------------------"> $ logfile
# Delete old file
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
Else
Case $ backupmethod in
Mysqldump *)
If [-Z $ dbpasswd]
Then
Mysqldump-U $ dbuser -- opt $ dbname> $ dumpfile
Else
Mysqldump-U $ dbuser-p $ dbpasswd -- opt $ dbname> $ dumpfile
Fi
Tar czvf $ newfile $ dumpfile >>$ logfile 2> & 1
Echo "[$ newfile] backup success! ">>> $ Logfile
Rm-RF $ dumpfile
;;
Mysqlhotcopy *)
Rm-RF $ dumpfile
Mkdir $ dumpfile
If [-Z $ dbpasswd]
Then
Mysqlhotcopy-U $ dbuser $ dbname $ dumpfile >>$ logfile 2> & 1
Else
Mysqlhotcopy-U $ dbuser-p $ dbpasswd $ dbname $ dumpfile >>$ logfile 2> & 1
Fi
Tar czvf $ newfile $ dumpfile >>$ logfile 2> & 1
Echo "[$ newfile] backup success! ">>> $ Logfile
Rm-RF $ dumpfile
;;
*)
/Etc/init. d/mysqld stop>/dev/null 2> & 1
Tar czvf $ newfile $ dbpath >>$ logfile 2> & 1
/Etc/init. d/mysqld Start>/dev/null 2> & 1
Echo "[$ newfile] backup success! ">>> $ Logfile
;;
Esac
Fi
Echo "-------------------------------------------"
Echo

# Lftp-F lftp. Sh if FTP is available, it can be uploaded to FTP at the same time.

 

 

Use the crontab command to generate the/var/spool/cron/root file.

 

Crontab-E (an editing window will be opened. The first line will contain a message in the content format. Enter the command here)

 

M h Dom mon Dow command (specific meaning: minute Hour Date month week command, in a month (Mon) One day (DOM) or the day of the day (Dow) (H, in a 24-hour system), a fraction (m) executes a command ))

 

For example, 10 02 ***/home/backup/autobackupmysql. Sh (indicates that the backup script is executed at 02:10 a.m)

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.