[Techtarget Chinese original]
Large companies usually have comprehensive backup policies. In small and medium-sized enterprises, when they really need it, they find that there is nothing on the tape used for backup. To avoid this, this article demonstrates how to create a simple backup solution for Linux.
The solution described in this article can be used to back up data files from a workstation computer to a central Linux server, or to write backup from one server to another. If you apply this solution to all servers on your network, you can provide a cheap and efficient company backup strategy, which can work well in small and medium-sized companies.
Large companies usually have comprehensive backup policies. In small and medium-sized enterprises, when they really need it, they find that there is nothing on the tape used for backup. To avoid this, this article demonstrates how to create a simple backup solution for Linux.
The solution described in this article can be used to back up data files from a workstation computer to a central Linux server, or to write backup from one server to another. If you apply this solution to all servers on your network, you can provide a cheap and efficient company backup strategy, which can work well in small and medium-sized companies.
Linux System Requirements
To implement this backup program, you need to prepare an external drive. You can write the backup to an external USB disk, but the efficiency is not high. Therefore, I assume that you write the backup to a server located somewhere on the network. This server should be a Linux server with SSH and rsync (available in any Linux release ).
In addition to a backup medium, you also need rsync. This multi-purpose synchronization tool is a default component of all Linux releases. As shown in its name, it will help you perform remote file synchronization. However, it does not perform incremental or differential backup. To prevent local hardware drive crashes and store data elsewhere, this tool can be used. You can also adjust the backup steps to perform incremental and differential backup, but this is not the purpose of this Article.
Now, we can easily understand the basic content of our backup solution. Rsync needs to communicate with the remote server and write data to the server. The default mechanism for rsync implementation is ssh. You may not do this, But SSH is available on almost all Linux machines, so you can use this tool exactly. The following command provides an example of the rsync command:
Rsync-RAU -- Progress/Data 192.168.1.98:/Data
With this command, rsync starts to synchronize the content in the local directory/data and the directory/data on the server 192.168.1.98. The connection to the server is established through the current user account. This means that if you are a user named Linda on the local workstation, you will use the account Linda to connect to the remote machine. If you are a root user, you will be connected as the root user. However, make sure that you have sufficient permissions to read the content of the source directory and write data to the target directory on the server. Secondly, the rsync command uses some parameters. -- The progress parameter displays the progress of the rsync command. The-RAU parameter ensures that everything is synchronized, including metadata in the file.
After the command is issued, the content in your local directory/data will be synchronized with the content in the/data directory on the server. However, the disadvantage of this command is that you must manually enter the command, and manual input means you may forget it. Therefore, we need to create a Cron task to make it work automatically. The problem with cron is that the connection to the server has been connected to the SSH daemon on the server, and the process requires a password. One of the optional solutions is to use a public/private key to configure SSH to automate the process.
Configure Automatic SSH Login
The idea of using a public/private key is to create a key pair, a public key, and a private key on your workstation. Next, copy the public key to ~ /. Ssh/authorized_keys file (~ The main directory of the current user ). Next time, when you start an SSH command on the workstation, it will first automatically attempt to connect through your public or private key pair. The workstation uses the private key to generate an encryption package. If the server can decrypt the package, the workstation can 100% confirm your identity, and you can get the authentication without entering the password. To create this configuration, perform the following steps:
- On the workstation, run the ssh-keygen-t dsa command and press enter only to accept all the default answers. In this way, the system will generate ~ /. Ssh/id_dsa file, which is your private key and ~ The/. Ssh/id_dsa.pub file serves as your public key.
- Now, use the ssh-copy-id command to copy the public key to the Home Directory of your server. The following command will help you achieve this purpose:
Ssh-copy-ID-I ~ /. Ssh/id_dsa.pub 192.168.1.98
By using this command, A. Ssh/authorized_keys file is created on the server, and you can log on through the public/private key pair.
After performing these steps, you can now log on to the remote server using the following command:
SSH 192.168.1.98
As you can see, you can log on without entering the password.
Schedule Backup Using Cron
Now, you know what commands you must use, and SSH has been set up to allow you to log on automatically. You need to tell your computer to automatically synchronize data every day. To help you do this, you can use cron on your workstation, which is another default component used in all Linux releases. To create a Cron task for your current account, you can use the crontab-e command. Open the crontab editor like VI or Joe. In the editor, enter the following command:
0 10 *** rsync-RAU/Data 192.168.1.98:/Data
As you can see, in the crontab file, the entered command is the same as the rsync command we used previously. The only difference is that the -- Progress parameter is omitted. Cron runs as a background task and is not connected to any terminal on the computer, so it cannot show you the progress.
Before running the actual command, you need to tell cron when to execute the command. To do this, I used 0 10 *** In the example command line ***. In Cron, when a task needs to be executed, it uses five locations to indicate the time. By running the preceding command, the task is run at 10 am every day. Remember to use 0 at 1st locations to specify the exact number of minutes for the task to run. If you forget to run the task from to, it will continue to run.
Here, we have introduced how to create a basic but effective backup program. Although there are many other solutions, the solution described in this article is one of the few simple and effective solutions. And it will always be more effective than the most common backup program for one-person companies and home users: There is no backup at all.