How VPS automatically backs up the database to FTP
Editor to share with you how VPS automatically back up the database to FTP, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
1) make sure you are in the home directory (if not, run: cd / home)
2) create a new backup folder (command: mkdir backup)
3) create a new script backup.sh
Vi backup.sh
Copy the following code (after copying: wq! To save):
#! / bin/bash
# what you want to change starts here
MYSQL_USER=root # mysql user name
MYSQL_PASS=123456 # mysql password
FTP_USER=vmvps # ftp user name
FTP_PASS=123456 # ftp password
FTP_IP=ftp.vmvps.com # 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 # website data to be backed up
# 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 corresponds to 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
# 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