Home >Operation and Maintenance >Linux Operation and Maintenance >Shell programming practice: backup of website files and database files

Shell programming practice: backup of website files and database files

齐天大圣
齐天大圣Original
2020-09-20 09:36:051408browse

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 will experience the despair of not being able to recover the data. The author has personally experienced it several times. The most serious one was when all the data on my personal blog for more than 7 months was lost. It was really a feeling that I wanted to cry without tears.

Today I will tell you how to write a script to back up website directories and database files on the server side. First, you need to add some knowledge:

  • tar command, file compression and packaging

  • find command, find files

  • 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. You can use the tar command to compress and package directories.

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

mysqldump -u用户名 -p密码 --all-databases | bzip2 > 备份目录/文件名

The website files need to be compressed using the tar command.

tar -jcpf 备份目录/文件名  需备份的目录

Although we perform backups every day, we only want to save the backup records of the last seven days, and the backup data 7 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 备份目录 -mtime +7 -type f -exec rm -f {} \;

Let’s start writing the shell script formally. First, we need to define the backup directory. If the backup directory does not exist, we need to create the directory first.

WWW_DIR=/home/wwwroot
BAK_DIR=/root/bak

if [ ! -d $BAK_DIR ];then
    mkdir $BAK_DIR
fi

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/null
mysqldump -uroot -p123456 --all-databases | bzip2 > $BAK_DIR/all_database_$(date +%Y%m%d).sql

Finally, just delete the backup data seven days ago

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

The complete code is posted below :

#!/bin/bash
# 备份数据库文件及网站文件

WWW_DIR=/home/wwwroot
BAK_DIR=/root/bak

if [ ! -d $BAK_DIR ];then
    mkdir $BAK_DIR
fi

# 备份网站文件及数据库文件
tar -jcpf $BAK_DIR/www_$(date +%Y%m%d).tar.bz2 $WWW_DIR 2>/dev/null
mysqldump -uroot -pGuiyuan#520@1314 --all-databases | bzip2 > $BAK_DIR/all_database_$(date +%Y%m%d).sql 

# 删除7天前的备份数据
find $BAK_DIR -type f -mtime +7 -exec rm -f {} \;

Finally, I hope everyone can develop the habit of backing up. In addition to daily backups on the server, it is also recommended to copy data from the server to the local disk once a week to ensure that the data is foolproof.

The above is the detailed content of Shell programming practice: backup of website files and database files. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn