The Debian system takes up less memory and is load balanced, but without control panel, management can only be managed through SSH using Linux commands. It's really troublesome for people unfamiliar with Linux commands. This article will detail step-by-step instructions on how to use Linux commands to automatically back up Web site data in a Debian system. (This is a backup method for all Linux systems that do not have to be Debian)
VPS System Environment
System description
VPS Host Memory 256MB, hard disk space 20GB; the system is 6 32-bit Debian; the installation is LNMP.
First, give a copy of the manual backup site:
cd/home/
mkdir Backup
CD Backup
mkdir www
mkdir MySQL
cd/root/
Touch backup.sh
chmod +x backup.sh
Vim backup.sh adds the following:
cd/home/backup/mysql/
Mysqldump-h localhost-u database username-p Database password database name | Gzip > Database name-$ (date +%y%m%d). sql.gz
Tar zcvf/home/backup/www/111cn.net_$ (date +%y%m%d). Tar.gz-c/home/wwwroot Zxsdw
If you want to manually back up your data, just run sh/root/backup.sh.
To automatically back up a run: The CRONTAB-E command adds the following: 0 2 * * * sh/root/backup.sh
Linux backup steps in detail:
Take 111cn.net as an example: the site is installed in the/home/wwwroot/111cn.net/directory, the backup file is intended to be placed under/home/backup/, in order to make the backup information clearer, back up the site files under/home/backup/www/, Put the MySQL database backup under/home/backup/mysql/.
Need to do in advance
Create the backup folder under/home/, and then create the WWW and MySQL folders under/home/backup/. After you log in using root, follow these steps
cd/home/
mkdir Backup
CD Backup
mkdir www
mkdir MySQL
Create a backup script
After using root login VPS, the default directory is/root/(if not, you can switch to the root directory with cd/root/), you can first run the following/root/file:
Touch backup.sh
chmod +x backup.sh
Vim backup.sh
Create a backup.sh execution file, set it to read and write through chmod, and edit the file with Vim.
Here's what you can add to your backup.sh through Vim, as follows:
1. Create a database backup script
cd/home/backup/mysql/
#将mysql的备份放到/home/backup/mysql/, so first CD to this directory.
Mysqldump-h localhost-u database username-p Database password database name | Gzip > Database name-$ (date +%y%m%d). sql.gz
#这段就是数据库中的各个数据库分开备份并且采用gzip压缩, export to a format such as "Database name-month day. sql.gz". GZIP compressed SQL database backup files Smaller, restore the data can be directly imported sql.gz backup files to restore.
2. Create a Web site file backup script
#绝对路径备份 (Popular point is to point to open the compression package first into the home, there are wwwroot, is one level open, the last is the folder of the Web site)
Tar zpcf/home/backup/www/111cn.net_$ (date +%y%m%d). tar.gz/home/wwwroot/111cn.net/
#相对路径备份 (compressed package only 111cn.net this folder and folder below the site data)
Tar zpcf/home/backup/www/111cn.net_$ (date +%y%m%d). Tar.gz-c/home/wwwroot 111cn.net
#以上为实例, revise according to your actual situation. This section 111cn.net site files are placed under the VPS/home/wwwroot/111cn.net/, and then use the TAR compression command to save all file backups under this file to the/home/backup/www/file, and then save as "111cn.net _ Month, day. tar.gz compressed file.
3. Create a site log backup script (optional)
Web site Access log is generally placed under the wwwlogs, the individual prefers to analyze the site log to understand the search engine crawling and search, so I want to separate back to the log of daily logs. Create the following script:
Tar zpcf/home/backup/log_$ (date +%y%m%d). tar.gz/home/wwwlogs/
In order to realize the daily daily backup of the site log, in order to understand the site crawling and recording every day, delete the original log after backing up the log, recreate a new blank log file, and add the following (take Www.111cn.net as an example):
Rm-rf/home/wwwlogs/*.*
cd/home/wwwlogs/
Touch Www.111cn.net.log
Add Delete old backup script
Use the VIM backup.sh command and add the following:
find/home/backup/-type f-mtime +2-exec rm-f {} \;
find/home/backup/www/-type f-mtime +2-exec rm-f {} \;
find/home/backup/mysql/-type f-mtime +2-exec rm-f {} \;
#这段为, delete the old backup files that were generated 2 days ago under/home/backup/and its subdirectories under WWW and mysql.
After you add the content to the backup.sh using vim editing, save the exit. Because backup.sh is placed in the/root/directory, the next Test runs:
sh/root/backup.sh
After running this command, if the error message does not appear, the backup script is created successfully, and then you can determine whether the backup was successful by looking at whether the backup file was generated under/home/backup/and its subdirectories.
Set up a VPS automatic scheduled backup
To implement automatic backups, you must use the Cron Timer task tool under Linux. Take my VPS system Debian6 32 system as an example to illustrate.
Add the following from the command crontab-e:
0 * * * * sh/root/backup.sh
Description: I set up a backup script to run 3 o'clock in the morning every day. But the test found that my Hong Kong VPS time seems to have time difference with Beijing. As above, I set 12 points corresponding to the running should be 3 o'clock in the morning Beijing time.
At this point, my Debian VPS was lnmp when the automatic backup site setup was successful. The implementation of 3 o'clock in the morning daily backup site files, database data and Web site access log, I can sftp from the/home/backup/download backup files
All right, if you guys have another server. We can use the SCP command to remotely back up or use the RSYCN Synchronization tool to sync, let's look at an example of an SCP backup file
Uploading local files to the server
Scp-p 2222/home/lnmp0.4.tar.gz root@www.111cn.net:/root/lnmp0.4.tar.gz
The upper port caps p is the parameter, 2222 indicates that the port after the SSH port is changed, and if the SSH port is not changed, you do not need to add the parameter. /home/lnmp0.4.tar.gz represents the path and file name of the file to be uploaded locally. Root@www.111cn.net indicates that using the root user to log on to the remote server www.111cn.net,:/root/lnmp0.4.tar.gz represents the directory and file name saved on the remote server.
4, upload the local directory to the server
Scp-p 2222-r/home/lnmp0.4/root@www.111cn.net:/root/lnmp0.4/
The upper port caps p is the parameter, 2222 indicates that the port after the SSH port is changed, and if the SSH port is not changed, you do not need to add the parameter. The-r parameter represents a recursive replication (that is, copying the files and directories under the directory);/home/lnmp0.4/represents the directory to be uploaded, root@www.111cn.net means to log on to the remote server using root www.111cn.net,:/root/ LNMP0.4/represents the directory location that is saved on the remote server.
5, a number of possible useful parameters:
-V is the same as the-V meaning in most Linux commands, and is used to show progress. Can be used to view connections, authentication, or configuration errors.
-C enables compression options.
-4 forcibly use IPV4 address.
-6 forcibly use IPV6 address.