Get the App
SLTechnology News&Howtos  ›  Development  › 

How VPS automatically backs up the database to FTP

Shulou Source: shulou.com Published: 2022-06-03 07:41:26 10月02日 Update

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

Tags: Data database file backup run article script content name command place password user user name directory space website not much code address Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi NVidia MySQL Shulou Information MariaDB