Automatic daily backup of LinuxVPS

August 13, 2017 2202 point heat 0 likes 3 comments

Recently, I used the VPS of Buyvm, which is very cost-effective, but because his family is operated by ONEMAN, he is afraid of running away. The loss of money is not important, just a few dollars. What about the data? If it is lost, it will be very interesting.

This paper introduces a method that automatically backs up website and database files every day, sends EMAIL to mailbox, uploads website and data and adds files to FTP space, and automatically deletes old backups.

First install the EMAIL sending component:

 yum install sendmail mutt

Script download address: http://down.vpsmm.com/shell/AutoBackupToFtp.sh

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

 #!/ bin/bash #The place you want to modify starts from here MYSQL_USER=root # mysql user name MYSQL_PASS=123456 # MySQL password MAIL_TO= cat@hostloc.com #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 backup files are stored on ftp. This must be created on ftp WEB_DATA=/home/www # 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 compressed file for each database 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 the FTP space and delete the data 5 days ago in the 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

To add execution permissions to a script:

 chmod +x /root/AutoBackupToFtp.sh

Use the system crontab to realize daily automatic operation:

 crontab -e

Enter the following:

 00 00 * * * /root/AutoBackupToFtp.sh

00 00 is the time minute/hour, which can be modified by itself, for example: 30 12 * * *, which 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 #Compact 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 #Use lftp to synchronize backup directories lftp -u user,password -e "mirror -R --only-newer /home/backup /backup" ftp.yoursite.cn exit END

 

This article is for collection only, the original text is transferred from @Xiaoye Blog

Gcod

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

Article comments

  • 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