Linux VPS automatic daily backup

August 13, 2017 1170 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

Script download address:

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

 #!/ bin/bash #Where you want to make changes, start here MYSQL_ User = root ා MySQL user name MYSQL_ Pass = 123456 ා MySQL password MAIL_ TO= #Mailbox to which the database is sent FTP_ User = cat ා FTP user name FTP_ Pass = 123456 ා FTP password FTP_ IP = imcat. In ා FTP address FTP_ Backup = backup ා the directory where the backup files are stored on FTP. This should be created on FTP WEB_ Data = / home / www # website data to be backed up #Where you want to change 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_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 #Send the database to email. If the database is too large after compression, please comment 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 and delete the data 5 days ago in FTP space ftp -v -n $FTP_ IP << END user $FTP_ USER $FTP_ PASS type binary cd $FTP_ backup delete $OldData delete $OldWeb put $DataBakName put $WebBakName bye END

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_$ (date -d -3day +"%Y%m%d").tar.gz #Export MySQL database /usr/local/mysql/bin/mysqldump -uroot -ppassword --databases db1 > /home/backup/databackup.sql #Compress database tar zcf /home/backup/$DataBakName /home/backup/databackup.sql rm -rf /home/backup/databackup.sql #Compress website data tar zcvf /home/backup/$WebBakName /home/wwwroot #Using LFTP to synchronize the backup directory lftp -u user,password -e "mirror -R --only-newer /home/backup /backup" exit END


This article is for collection only @Xiaoye blog


If life is just like the first sight, what is the sad autumn wind painting fan

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