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 use Shell script in CentOS to automatically back up website files and databases every day and upload them to FTP

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to use Shell scripts in CentOS to automatically back up website files and databases every day and upload them to FTP". The content in the article is simple and clear, and it is easy to learn and understand. please follow the editor's ideas to study and learn "how to use Shell scripts in CentOS to automatically back up website files and databases every day and upload them to FTP".

Install the Email sender

The code is as follows:

Yum install sendmail mutt

Second, install FTP client program

This script needs to use the FTP client program to upload the file to the FTP space, so you must install the FTP client, or there will be an error that the ftp command cannot find.

For installation steps, please refer to "CentOS prompt ftp:command not found"

3. Create a new script file: AutoBackupToFtp.sh in the / root directory, with the following contents:

The code is as follows:

#! / bin/bash

# what you want to change starts here

MYSQL_USER=root # mysql user name

MYSQL_PASS=123456 # mysql password

MAIL_TO=xxxxx@gmail.com # mailbox to which the database is sent

FTP_USER=ftpuser # ftp user name

FTP_PASS=ftpuserpassword # ftp password

FTP_IP=xxx.xxx.xxx.xxx # ftp address

The directory on FTP_backup=backup # ftp where backup files are stored. You must build this directory on ftp.

WEB_DATA=/home/wwwroot # the website data to be backed up. If you use the lnmp installation package, this is the website directory by default.

# the place you want to modify ends here

# define the name of the database and the name of the old database

DataBakName=Data_$ (date + "% Y%m%d") .tar.gz

WebBakName=Web_$ (date +% Y%m%d) .tar.gz

OldData=Data_$ (date-d-5day + "% Y%m%d") .tar.gz

OldWeb=Web_$ (date-d-5day + "% Y%m%d") .tar.gz

# Delete local data 3 days ago

Rm-rf / home/backup/Data_$ (date-d-3day + "% Y%m%d"). Tar.gz / home/backup/Web_$ (date-d-3day + "% Y%m%d") .tar.gz

Cd / home/backup

# Export database, one database and one compressed file

For db in `/ usr/local/mysql/bin/mysql-u$MYSQL_USER-p$MYSQL_PASS-B-N-e 'SHOW DATABASES' | xargs`; do

(/ usr/local/mysql/bin/mysqldump-u$MYSQL_USER-p$MYSQL_PASS ${db} | gzip-9-> ${db} .sql.gz)

Done

# compress the database file into one file

Tar zcf / home/backup/$DataBakName / home/backup/*.sql.gz

Rm-rf / home/backup/*.sql.gz

# send the database to Email. If the database is too large after compression, please comment on this line

Echo "Content:This email is auto send by vps..." | mutt-a / home/backup/$DataBakName-s "Subject:VPS Database Backup" $MAIL_TO

# compress website data

Tar zcf / home/backup/$WebBakName $WEB_DATA

# upload to FTP space and delete data from 5 days ago in FTP space

Ftp-v-n $FTP_IP

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

Development

Wechat

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

12
Report