Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Shell script in CentOS to automatically back up website files and databases every day and upload them to FTP

Shulou Source: shulou.com Published: 2022-06-03 09:19:39 10月04日 Update

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

Tags: Data database file website script backup code content client client directory program space learning name place password user user name command Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information vpn NVidia Shulou Tech Info Huawei