Linux VPS automatic daily backup

August 13, 2017 801 point heat 0 liked it 3 comments

Recently, I used buyvm's VPS, which is very cost-effective, but because his family is operated by oneman, he is afraid of running away. The loss of money doesn't matter. It's only a few dollars. But what about the data? If it's lost, it's fun.

This paper introduces a method of automatically backing up website and database files every day, sending email to mailbox, uploading website and data, adding files to FTP space, and automatically deleting old backup.

First install the email sending component:

 yum install sendmail mutt

Download script address:

The script code is as follows (pay attention to modify the FTP server address, user name and password)

 #! / bin / bash 
 # the place you want to modify starts here 
 MySQL_ User = root # MySQL user name 
 MySQL_ Pass = 123456 ා MySQL password 
 mail_ To = cat @ # email sent by database 
 FTP_ User name_ Pass = 123456 ා FTP password 
; FTP_ IP = # FTP address 
 FTP_ Backup = backup ා the directory where the backup files are stored. You have to create a 
 web on FTP_ Data = / home / www # the 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, 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_ Compress the database file to a file, tar ZCF / home / backup / $databakname / home / home / backup / $databakname / home / backup / / home / backup / / / home / backup / * sql.gz / * sql.gz sendingdatabase to email, if the database is compressed too big, please comment this line comments this line.
echo "subject: database backup" | mutt - A / home / backup / $databakname - s "content: database backup" $mail_ To compress website data tar ZCF / home / backup / $webbakname $Web_ Data 
 # upload to FTP space, delete data 5 days ago in FTP space 
 FTP - V - N $FTP_ IP << END
type binary
cd $FTP_ backup
delete $OldData
delete $OldWeb
put $DataBakName
put $WebBakName

Add execution permission to script:

 chmod +x /root/

The system crontab is used to realize automatic operation every day

 crontab -e

Enter the following:

 00 00 * * * /root/

00 00 is the time minute / hour, which can be modified by yourself. For example, 30 12 * * means running the script at 12.30 every day.

The following is the LFTP backup script:

 #! / bin / bash 
 # 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 
 # delete local data 3 days ago 
 RM - RF / home / backup / data_ $(date -d -3day +"%Y%m%d").tar.gz /home/backup/Web_ Export MySQL database 
 / usr / local / MySQL / MySQL / bin / MySQL dump - urot - ppapassword -- databases db1 > / home / backup / databackup.sql 
 SQL compression database compression database compression: Tar ZCF / home / home / backup / backup / $databakname / home / backup / backup / databackup.sql \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\/ home / backup / DataBackup. SQL 
 # compress website data 
 tar zcff / home / backup / $webbakname /Home / wwwroot 
 use LFTP to synchronize backup directory 
 LFTP - U user, password - e "mirror - R -- only newer / home / backup / backup" 


This article is for collection only @Xiaoye blog


Talking about the trivial matters of life and talking about the whole world, that's all

Article review

  • youjizz

    Yߋu are so awesome! Ι don't suppose I havee гead
    a single thіng like that ƅefore. So wonderful tߋo
    discovver anotһer person ᴡith original thοughts on thiѕ topic.
    Seriously.. thаnks for starting tһis up. Thіs wweb site іs one tһing that is needeⅾ on the
    internet, ѕomeone with a bit of originality!

    February 5, 2018
  • youjizz videos

    Hello colleagues, іts wonderful article on the topic
    օf cultureand completeely defined, кeep it uр
    alll thе time.

    January 30, 2018
  • Ji Changxin
    August 15, 2017