Automatic backup of website and database scripts under CentOS

  
                  

I have been in contact with CentOS for a few months. Windows that has been used for a long time is really a little uncomfortable when I first came into contact with the Linux command line interface. But in the end, I should have everyone’s words. The longer the linux contact, the more conducive to her simplicity and efficiency, I have a server installed operating system is CentOS5.4, deployed on the site and database applications, the basic framework: centos+nginx+mysql+ Php, but because the website data is more, it is too time-consuming to manually back up, so I plan to do an automatic running script to help me back up the website content and database content at the specified time of the week, package it with tar, and then pass it to On my other ftp server, the entire backup process is complete.

Assume that the website directory of this server is: /home/www, the database program path is: /usr/local/mysql/bin, the database name is: levil, and the ftp server is: ftphost. Complete automatic backup script (automatic backup script save location: /home/backup.sh):

#!/bin/bash cd /home WebBakName=web_$(date +%y%m%d) .tar.gz tar zcvf $WebBakName www SqlBakName=sql_$(date +%y%m%d).tar.gz /usr/local/mysql/bin/mysqldump -uusername -ppassword levil>backup.sql tar zcvf $SqlBakName Backup.sql ftp -nv ftphost << EOF user ftpname ftppass put $WebBakName put $SqlBakName quit EOF rm -f backup.sql $WebBakName $SqlBakName

Let's explain it to everyone: First enter /Home directory, define the WebBakName variable for the file name of the website backup, the file name format is web_date.tar.gz, define the SqlBakName variable as the file name of the database backup, the file name format is sql_date.tar.gz, put the entire website The directory www is packaged into the WebBakName file name, use mysqldump to export the specified database contents to backup.sql Then the database backup is packaged into the SqlBakName file, the local backup work ends here. If you do not have enough remote ftp space, you can directly download the backup file to the machine, but I still recommend directly backing up to another server. In the ftp space, to fully automate the backup, you need to replace the ftphost, ftpname and ftppass in the script with your ftp information, and the entire backup process is completed.

Then enter chmod +x backup.sh to add execute permission to the script, then enter: crontab -e to edit the task automatic start time, for example, I type: 00 05 * * 1 /home/backup.sh means each The automatic backup operation is performed at 5 am on week 1.

After all this is done, you can change the automatic task time to the current proximity time to see if the automatic backup script works normally. If you test OK, you don't have to worry about any problems with this server. Of course, if your data is updated frequently, it is recommended to adjust the automatic backup time to daily.

Copyright © Windows knowledge All Rights Reserved