Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to back up the database regularly using Linux

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to use Linux to back up the database regularly. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

First, configure mailboxes

Here is the use of NetEase mailbox 126mailbox STMP service, the server is smtp.126.com. If you use other third-party mailboxes, the SMTP server address is usually specified in the account settings.

If your Ubuntu does not have the mail module installed, you need to execute this command to install the mail module:

Root@name:~# apt-get install mailutils

Open the mail configuration file

Vim / etc/mail.rc

Add these settings at the end

Set from=test@126.com smtp=test@126.comset smtp-auth-user=test@126.com smtp-auth-password=mypassword smtp-auth=login

Test whether the message was sent successfully:

Echo "test content" | mail-s "mail title" myqq@qq.com

Explanation: test is the content of the email, mail title is the title of the email, and xxxx@qq. Com indicates that you want to send it to this email address. Successfully received the e-mail, no problem.

2. Back up Mysql database

Use the mysqldump command to backup Mysql. Familiarize yourself with it and improve it later.

Mysqldump-u database account-p database password database name > "/ tmp/bak.sql"

Execute the above command and enter the / tmp/bak.sql backup file to see if it exists or not. Note that there is no space between the-p database passwords.

Write scripts and scheduled tasks

When everything is ready, the next step is to automate: create a backup script and execute it every day using scheduled tasks.

Create a SHELL script in a directory. For a simple explanation, I directly build it in / back_mysql.sh, as follows

Vim / back_mysql.sh

#! / bin/bash#Send the mysql of backup file to email#Author Melon#Version 0.Qing name backup file filename= `date +% YMY% mmury% d` "- blog-bak.sql" path= "/ tmp/" $filename# to perform backup / usr/bin/mysqldump-u database account-p database password database name > $path# send email # Delete temporary file echo "email content" | mail-s $filename-a $path mailbox rm-f $path

/ usr/bin/mysqldump is the complete command path. If you don't know the full path of the command, you can use the whereis command name to view the scheduled tasks created using crontab, which are executed at 00:00 every day.

Crontab-e0 0 * / bin/sh / back_mysql.sh

You can set other time periods to send. Please search for the specific usage of crontab.

Use the zip command to package and encrypt the file

You may feel that it is not safe to send sql files directly, or there are other files such as attachments to be sent together. Next, I will use the linux zip command to encrypt and package the attachments and sql files. The complete code is as follows, which will not be explained in detail.

#! / bin/bash#Send the blog of backup file to email#Author Melon#Version 0.2#zip file name filename= `date +% Ymuri% mmury% d` "- blog-bak.zip" # zip file path path= "/ tmp/" $filename#sql backup file path mysqlpath= "/ tmp/blog.sql" # attachment backup path uploadpath= "/ tmp/uploads" / usr/bin/mysqldump-u database account-p database password database name > $mysqlpath# Create an attachment backup (this is the attachment location of my blog) cp-r "/ www/blog/usr/uploads/" / tmp/ "# encryption package > / dev/null 2 > & 1 means that the message is redirected so that encrypted information is not output during debugging. You can also omit the / usr/bin/zip-P encryption password-r $path $mysqlpath $uploadpath > / dev/null 2 > & 1echo "email content" | mail-s $filename-a $path mailbox # Delete the backup file rm-f $path $mysqlpathrm-rf $uploadpath what is the Linux system Linux is a free-to-use and free-spread UNIX-like operating system Is a POSIX-based multi-user, multi-tasking, multi-threaded and multi-CPU operating system, using Linux to run major Unix tools, applications and network protocols.

On the use of Linux how to regularly backup the database to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report