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 backup website Files and Database Files in shell programming practice

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

Share

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

This article mainly introduces the shell programming practice of the website files and database files how to backup, the article is very detailed, has a certain reference value, interested friends must read it!

The importance of file backup is self-evident, regular backup is a good habit, whether it is website files or daily life photos, videos, etc. Otherwise, when the data is lost, you can feel the despair that the data can not be found. The author has personally experienced several times, the most serious one is the personal blog for more than 7 months of data loss, it is really a feeling of tears.

Today, let's talk about how to write a script to back up website directories and database files on the server side. First of all, we need to add a few pieces of knowledge:

Tar command, compression and packaging of files

Find command, finding files for

Bzip2, file compression

In order to save space, we often compress and package directory files. The bzip2 command can compress files, but it cannot compress directories, and directories can be compressed and packaged using the tar command.

Because the database file is a separate sql file, you can use the bzip2 command to compress it.

Mysqldump-u user name-p password-- all-databases | bzip2 > backup directory / file name

Web site files need to be compressed using the tar command.

Tar-jcpf backup directory / filename directory to be backed up

Although we make backups every day, we only want to save the backup records for the last seven days, and the backup data from seven days ago will be automatically deleted. Here we need to use the find command to find the backup file from 7 days ago, and then delete it.

Find backup directory-mtime + 7-type f-exec rm-f {}\

Let's start writing a formal shell script. First, we need to define the backup directory, and if the backup directory does not exist, we need to create the directory first.

WWW_DIR=/home/wwwrootBAK_DIR=/root/bakif [!-d $BAK_DIR]; then mkdir $BAK_DIRfi

The next step is to back up the website directory and database files.

Tar-jcpf $BAK_DIR/www_$ (date +% Y%m%d). Tar.bz2 $WWW_DIR 2 > / dev/nullmysqldump-uroot-p123456-- all-databases | bzip2 > $BAK_DIR/all_database_$ (date +% Y%m%d) .sql

Finally, just delete the backup data from seven days ago

Find $BAK_DIR-type f-mtime + 7-exec rm-f {}\

The complete code is posted below:

#! / bin/bash# backup database files and website files WWW_DIR=/home/wwwrootBAK_DIR=/root/bakif [!-d $BAK_DIR] Then mkdir $BAK_DIRfi# backup website files and database files tar-jcpf $BAK_DIR/www_$ (date +% Y%m%d). Tar.bz2 $WWW_DIR 2 > / dev/nullmysqldump-uroot-pGuiyuan#520@1314-- all-databases | bzip2 > $BAK_DIR/all_database_$ (date +% Y%m%d). Sql # deletes the backup data find $BAK_DIR-type f-mtime + 7-exec rm-f {}\

Finally, I hope you can get into the habit of backup. In addition to the daily backups on the server, it is also recommended to copy a copy of the data from the server to the local disk for a week to ensure that the data is foolproof.

The above are all the contents of shell programming practice website files and database files how to back up, thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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