• Creating a simple backup scheme for Linux using rsynch, SSH, and cron

  • von: Senaida680 hinzugefuegt: 14-06-18
  • This article demonstrates how to create a simple backup solution for Linux.

    The solution described in this article can be used in two ways: write backups of data files from a workstation computer to a central Linux server, or write backups from one server to another. Applying this solution to all servers in your network can provide you with a cheap and efficient company backup strategy that works well for small and medium sized companies.

    Linux system requirements

    In order to implement this backup program, you need to prepare an external drive. You can write backups to an external USB disk, but this is inefficient. Therefore, I assume that you write the backup to a server somewhere on the network. This server should be a Linux server with SSH and rsync (any Linux distribution can).

    In addition to a backup media, you also need rsync. This versatile synchronization tool is a default component on all Linux distributions. As its name suggests, it will help you to synchronize files remotely. However, it does not perform incremental or differential backups. This tool works when you want to prevent the local hardware drive from crashing and want to store the data elsewhere. You can also adjust the backup steps to perform incremental and differential backups, but this is not the purpose of this article.

    Now, it is easy to understand the basic content of our backup solution. Rsync needs to communicate with a remote server and write data to the server. The default mechanism used by Rsync to achieve this is SSH. You can not do this, but SSH is available on almost all Linux machines, so you can use this tool exactly. The following command gives an example of the rsync command:

    Rsync -rau --progress /data

    With this command, rsync starts to synchronize the contents of the local directory /data and the directory /data on the server The connection to the server is established through the current user account. This means that if you are a user on the local workstation called linda, then you will connect the remote machine with the linda account. If you are a root user, you will connect as the root user. However, make sure you have enough privileges to read the contents of the source directory and write to the target directory on the server. Second, the rsync command uses some parameters. The --progress parameter shows the progress of the rsync command, and the -rau parameter ensures synchronization of everything, including the metadata in the file.

    After issuing this command, the contents of your local directory /data will be synchronized with the contents of the /data directory on the server. However, the disadvantage of this command is that you must enter it manually, and manual input means that you may forget it. So we need to let it work automatically by creating a cron task. The problem with cron is that it establishes a connection to the server and is connected to the SSH daemon on the server, and the process asks for a password. One of the alternative solutions is to configure SSH using public and private keys 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 the ~/.ssh/authorized_keys file on the server (~ refers to the current user's home directory). The next time, when you start an SSH command on a workstation, it will automatically try to connect through your public or private key pair first. The workstation uses a private key to generate an encrypted packet. If the server can decrypt it, the workstation can identify you 100% and you can be authenticated without entering a password. To create this configuration, you should perform the following procedure:

    On the workstation, use the ssh-keygen –t dsa command and press Enter to accept all the default responses. In this way, the system will generate a file called ~/.ssh/id_dsa, which is your private key, and a ~/.ssh/id_dsa.pub file, as your public key. Now use the ssh-copy-id command to copy the public key to your server's home directory. The following command will help you achieve this goal:

    Ssh-copy-id -i ~/.ssh/id_dsa.pub

    By using this command, an .ssh/authorized_keys file is created on the server and allows you to log in through public/private key pairs.

    After performing these steps, you can now log in to the remote server with the following command:


    As you can see, now you can log in without entering a password.

    Use cron to schedule backups

    Now that you know what commands you must use, SSH has also been set up so that you can log in automatically. You need to tell the computer to automatically synchronize data every day. To help you do this, you can use cron on your workstation, which is another default component that is used in all Linux distributions. In order to create a cron task for your current account, you can use the crontab –e command. Open a crontab editor such as vi or joe. In the editor, enter the following command:

    0 10 * * * rsync -rau /data

    As you can see, the command entered in the crontab file is the same as the rsync command we used earlier. The only difference is that the --progress parameter is omitted. Because cron is run as a background task and is not connected to any terminal on the computer, it can't show you progress.

    Before the actual command runs, you need to tell cron when to execute the command. To do this, in the example command line, I used 0 10 ***. In cron, when a task needs to be executed, 5 locations are used to indicate the time. By using the above command, the task will be run at 10 am every day. Remember to use 0 in the first position to specify the exact number of minutes the task will run. If you forget, from 10:00 to 10:59, the task will be running.

    Here, we have made a full introduction to how to establish a basic but effective backup program. Although there are many other solutions, the solution presented in this article is one of the few simple and effective solutions. And compared to the most common backup program for one person company and home user: It is always more effective if it is not backed up at all.

    If you have any inquiries regarding where and how to use usb backup image, you can call us at the internet site.

  • (0) (0 Votes)
  • Antwort   |   melden
  • blink    digg    furl    furl    furl    furl    deli    BlogMarks    Magnolia    StumbleUpon    RSS Subscribe

Powered Free by PHPmotion - Video Script