Linux VPS and server website and database automatically local backup and FTP upload backup scripts

  • 2020-05-06 12:02:07
  • OfStack

Preparation:

lftp needs to be installed in VPS in advance, lftp is relatively powerful in function, CentOS directly execute: yum install lftp, Debian execute: apt-get install lftp.

You need to create the /home/backup/ directory on VPS and the backup directory on FTP.

If you don't have many databases on VPS, you can use Godaddy's free space (10GB space, 300GB traffic), and just register a domain name for free.

The backup script is partially commented below:
 
#!/bin/bash 
#Funciont: Backup website and mysql database 
#Author: licess 
#Website: http://lnmp.org 
#IMPORTANT!!!Please Setting the following Values! 
######~Set Directory you want to backup~###### Modify the following directory to their own backup directory, generally press my are in /home/wwwroot/ The following directories are all directly written to backup. Can continue to add: Backup_Dir5= Your directory   . Backup_Dir The following Numbers are increasing. If less than 4 One, directly delete do not need to be able to, at the same time modify the following tar zcf  Part.  

Backup_Dir1=vpser.net 
Backup_Dir2=lnmp.org 
Backup_Dir3=licess.org 
Backup_Dir4=jungehost.com 

######~Set MySQL UserName and password~###### Set up the MySQL Username and password, preferably root , other users may not be able to export part of the database due to permission issues.  
MYSQL_UserName=root 
MYSQL_PassWord=yourmysqlrootpassword 

######~Set MySQL Database you want to backup~###### To set up the database, you can continue to add: Backup_Database_Name5= Database name, Backup_Database_Name The following Numbers are increasing.  
Backup_Database_Name1=vpser 
Backup_Database_Name2=licess 
Backup_Database_Name3=junge 
Backup_Database_Name4=vpserorg 

######~Set FTP Information~###### Set for storing backup data FTP information  
FTP_HostName=184.168.192.43 //FTP The server's IP Or the domain name  
FTP_UserName=vpsernet //FTP Server username  
FTP_PassWord=yourftppassword //FTP The password for the server user  
FTP_BackupDir=backup // Backup to FTP On the directory, need to be created in advance.  

#Values Setting END! 

TodayWWWBackup=www-*-$(date +"%Y%m%d").tar.gz 
TodayDBBackup=db-*-$(date +"%Y%m%d").sql 
OldWWWBackup=www-*-$(date -d -3day +"%Y%m%d").tar.gz 
OldDBBackup=db-*-$(date -d -3day +"%Y%m%d").sql 

tar zcf /home/backup/www-$Backup_Dir1-$(date +"%Y%m%d").tar.gz -C /home/wwwroot/ $Backup_Dir1 --exclude=soft 
tar zcf /home/backup/www-$Backup_Dir2-$(date +"%Y%m%d").tar.gz -C /home/wwwroot/ $Backup_Dir2 
tar zcf /home/backup/www-$Backup_Dir3-$(date +"%Y%m%d").tar.gz -C /home/wwwroot/ $Backup_Dir3 --exclude=test 
tar zcf /home/backup/www-$Backup_Dir4-$(date +"%Y%m%d").tar.gz -C /home/wwwroot/ $Backup_Dir4 

### Above for backup site file data, because my site is relatively scattered, and the site directory below some directories are temporary directory does not need to backup, so you can add on top --exclude= Directory not backed up. If I add it in front Backup_Dir5=yourdir , then add tar zcf /home/backup/www-$Backup_Dir5-$(date +"%Y%m%d").tar.gz -C 
/home/wwwroot/ $Backup_Dir5  . Delete the extra rows if they are redundant.  

/usr/local/mysql/bin/mysqldump -u$MYSQL_UserName -p$MYSQL_PassWord $Backup_Database_Name1 > /home/backup/db-$Backup_Database_Name1-$(date +"%Y%m%d").sql 
/usr/local/mysql/bin/mysqldump -u$MYSQL_UserName -p$MYSQL_PassWord $Backup_Database_Name2 > /home/backup/db-$Backup_Database_Name2-$(date +"%Y%m%d").sql 
/usr/local/mysql/bin/mysqldump -u$MYSQL_UserName -p$MYSQL_PassWord $Backup_Database_Name3 > /home/backup/db-$Backup_Database_Name3-$(date +"%Y%m%d").sql 
/usr/local/mysql/bin/mysqldump -u$MYSQL_UserName -p$MYSQL_PassWord $Backup_Database_Name4 > /home/backup/db-$Backup_Database_Name4-$(date +"%Y%m%d").sql 

### Above is a backup MySQL Database, if I add it in front Backup_Database_Name5=yourdatabasename , then add /usr/local/mysql/bin/mysqldump -u$MYSQL_UserName -p$MYSQL_PassWord $Backup_Database_Name5 > /home/backup/db-$Backup_Database_Name5-$(date +"%Y%m%d").sql  . Delete the extra rows if they are redundant.  

rm $OldWWWBackup 
rm $OldDBBackup 
### delete 3 Days ahead backup ### 

cd /home/backup/ 

### The following is the part of automatic upload, I have to say lftp Very strong, discard ftp ! #### 
lftp $FTP_HostName -u $FTP_UserName,$FTP_PassWord << EOF 
cd $FTP_BackupDir 
mrm $OldWWWBackup 
mrm $OldDBBackup 
mput $TodayWWWBackup 
mput $TodayDBBackup 
bye 
EOF 

Most of the above is in the form of code, for beginners may be difficult to understand, see the confusion, or serious learning will be able to have a harvest, I hope that the content mentioned above can be helpful to you.

Related articles: