As a new-generation network operating system, Linux is widely used in servers. As a dedicated network server, an important function is to back up server data to ensure data security. This article describes some common data backup methods in Linux.
1. Manual data backup on the local machine
The Linux system is equipped with powerful tar commands to flexibly back up data. Tar was originally designed to back up files and directories to tape for tape backup and then extract or restore files from the tape. Of course, now we can use tar to back up data to any storage medium. Tar is very easy to use, stable, and reliable, and has this command on any Linux system. This is the most frequently used backup tool.
(1) Use the tar command to back up data in the following format:
$ Tar cvf backup.tar/home/html
The preceding command is to pack all the files in the/home/html Directory into the tarfile backup.tar.
Cvf is the command parameter of tar.
C Indicates creating an archive file,
V indicates the name of each backup file,
The file name created by f tar.tar is backup.tar,
/Home/html indicates the name of the file or directory to be backed up by tar.
(2) Use the tar command to restore data in the following format:
$ Tar xvf backup.tar
Run the following command to restore the Upload File backup.tar to the current directory.
Generally, tar does not compress files when backing up files. Therefore, the size of the backup files is very large. Run the following command to compress the backup file after the backup is complete.
$ Tar zcvf backup.tar.gz/home/html
In this case, we can get the compressed file backup.tar.gz.
Backup.tar.gz is a compressed backup file.
2. Automatic Backup of local data
Using the above command, we can manually back up data, but daily scheduled operations may be cumbersome. Of course, Linux provides us with powerful tools for automatic backup. This is cron.
Cron is a background process. once started, the task will be executed regularly according to the configuration file. We can write a shell script file to back up the file, and then let cron regularly start the script file to back up the data.
For example, if we back up all the files in the/home/html directory to the/home/admin/backup/backup_xxxx directory every day, xxxx indicates the backup date. You can write the following shell script backup. sh for this task:
#! /Bin/sh
Cd/home/admin/backup
Year = 'date + % y'
Month = 'date + % m'
Day = 'date + % d'
Now = $ year-$ month-$ day
Mkdir backup _ $ now
Tar zcvf backup _ $ now/backup.tar.gz/home/html
This script automatically reads the system date when backing up data, creates a new directory based on the current system date, and packs and compresses the backup data in this directory. We can execute $ sh backup. sh in the system to complete the backup. If you type $ chmod + x backup. sh, you can set backup. sh to run. In this way, you can directly run the backup script file by typing $./backup. sh. Generally, we want to back up data when the system load is not the largest. Generally, we can back up data at every day (during this time, the number of users accessing your server should be minimal ?). Give everything to cron. After cron is started, it checks the configuration file in the/var/spool/cron/directory to find the task to be executed and the time to execute the task, according to these settings, the specified task is executed at the specified time.
The task configuration file can be edited by the crontab-e command.
The format is that each line represents a task and the specified execution time.
Each line consists of six fields:
Minute hour day month week command
The preceding six fields are separated by spaces or tabs:
Minute: minute field. The value ranges from 0 to 59.
Hour: small time domain, value range: 0 to 23
Day of each month: date. The value ranges from 1 to 31.
Month: month. The value ranges from 1 to 12.
Week: Week, the value range is 0 to 6, and the Sunday value is 0.