Linux server backup: VPS backup script

  
                  Since the beginning of the VPS Detective, it has been reiterated that it must back up its own data, because too much uncertainty may cause database loss, and most VPS service providers are not likely to provide daily backup data.

The original VPS detective provided a VPS backup method, just a little said, maybe some newcomers will not start well. Today VPS Detective shares a backup script of its own.

Preparation:

You need to install lftp in VPS in advance, lftp function is more powerful, CentOS directly executes: yum install lftp, Debian execution: apt-get install lftp.

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

If there are not many databases on the VPS, use Godaddy's free space (10GB space, 300GB traffic), as long as you register a domain name for free.

The following is a partial comment on the backup script:


#!/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~###### The directory is changed to the directory that you want to back up. Generally, all of them are written under /home/wwwroot/directly to the directory that needs to be backed up. You can continue to add: Backup_Dir5=Your directory, the number after Backup_Dir is incremented. If there are less than 4, you can delete the unnecessary ones directly, and modify the tar zcf part below.

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

######~Set MySQL UserName and password~######Set the MySQL username and password, preferably root. Other users may not be able to export some databases due to permissions issues.
MYSQL_UserName=root
MYSQL_PassWord=yourmysqlrootpassword

######~Set MySQL Database you want to backup~######Set the database to be part of, you can continue to add: Backup_Database_Name5=Database name, the number following Backup_Database_Name is incremented.
Backup_Database_Name1=vpser
Backup_Database_Name2=licess
Backup_Database_Name3=junge
Backup_Database_Name4=vpserorg

######~Set FTP Information~######Settings are used to FTP information for storing backup data
FTP_HostName=184.168.192.43 //FTP server IP or domain name
FTP_UserName=vpsernet //FTP server username
FTP_PassWord=yourftppassword //FTP server user password
FTP_BackupDir=backup //Back up to the directory on FTP and need to create it 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< Br>

###The above is the backup website file data, because my website is scattered, and some directories under the website directory belong to the temporary directory and do not need to be backed up, so you can add --exclude= not backed up the directory . If Backup_Dir5=yourdir is added in front, add tar zcf /home/backup/www-$Backup_Dir5-$(date +"%Y%m%d").tar.gz -C
/home/wwwroot /$Backup_Dir5 . If it is redundant, delete the extra line.

/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

###The above is the backup MySQL database, if you add Backup_Database_Name5=yourdatabasename in front, 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 . If it is redundant, delete the extra line.

rm $OldWWWBackup
rm $OldDBBackup
###Remove backups 3 days ago###

cd /home/backup/

## #以下为Automatic upload part, I have to say that lftp is very powerful, abandon ftp####
lftp $FTP_HostName -u $FTP_UserName, $FTP_PassWord << EOF
cd $FTP_BackupDir
mrm $ OldWWWBackup
mrm $OldDBBackup
mput $TodayWWWBackup
mput $TodayDBBackup
bye
EOF


Script download address: http://soft.vpser .net/lnmp/backup.sh

Download the script, put the script under /root/, modify the parameters in the script according to the above comments, and save it. If you are not familiar with vi or nano editor, you can use Winscp, execute: crontab -e

Add in crontab: 0 3 * * * /root/backup.sh

Automatically execute /root/bakcup.sh script at 3 am, backup vps The data on it is uploaded to FTP.

Copyright © Windows knowledge All Rights Reserved