Jak zrobić kopię zapasową lokalnego katalogu na zdalny serwer w systemie Linux

Linux is the most flexible operating system available on the market. With this open-source platform, you can do much more than with a proprietary software operating system, without spending money on licenses.

If you’re just starting out with Linux, you probably want to avoid using the command line because it can be a bit intimidating. However, as you develop your skills, you’ll want to harness the full power and flexibility that this system offers. One very useful task you can accomplish is creating backups of a local directory on a remote server using the rsync command. Here’s how to do it.

Start by installing the rsync program using the following command:

sudo apt-get install rsync -y

Next, configure rsync on the remote server. To do this, create a new configuration file by entering the command:

sudo nano /etc/rsyncd.conf

Paste the following content into this file:

[backup]
path=REMOTE_DIRECTORY
hosts allow = LOCAL_IP_ADDRESS
hosts deny = *
list = true
uid = root
gid = root
read only = false

Note that REMOTE_DIRECTORY is the name of the directory on the remote server that will store the backups, and LOCAL_IP_ADDRESS is the IP address of the local computer.

After saving and closing the file, configure and start rsync using the following command:

sudo systemctl enable –now rsync

Now, test the backup process. On the local computer, run the rsync command:

rsync -avz LOCAL_DIRECTORY REMOTE_SERVER_IP_ADDRESS::backup

Where LOCAL_DIRECTORY is the directory you want to copy, and REMOTE_SERVER_IP_ADDRESS is the IP address of the remote server. Remember to specify the backup name (::backup) you used in the configuration file on the remote server.

Automate the backup process. With the built-in cron tool, you can easily do this by creating a bash script for the backup. Create a file named rsync.sh and enter the same command you used earlier, adding the q option to silence the output:

rsync -avzq LOCAL_DIRECTORY REMOTE_SERVER_IP_ADDRESS::backup

Save and close the file. Give permissions to execute the script using the command:

chmod u+x rsync.sh

Then, create a cron job with the command:

sudo crontab -e

Paste the following content into the file:

00 01 * * * /home/USER/rsync.sh

Note: Replace USER with your username. Save and close the file.

Now, your new cron job will run daily at 01:00, ensuring you have a fresh backup of this directory.

As you can see, creating a basic backup on a remote server in Linux is quite simple.

Q&A:
1. Why is it worthwhile to use Linux?
Linux is the most flexible operating system available on the market. It allows for greater possibilities than proprietary software operating systems, without requiring spending money on licenses.

2. How do I install the rsync program?
To install the rsync program, use the command sudo apt-get install rsync -y.

3. How do I configure rsync on a remote server?
To configure rsync on a remote server, create a new configuration file using the command sudo nano /etc/rsyncd.conf. Then paste the appropriate content into the file.

4. How do I create a backup on a remote server using rsync?
To create a backup on a remote server using rsync, run the rsync command on the local computer using rsync -avz LOCAL_DIRECTORY REMOTE_SERVER_IP_ADDRESS::backup.

5. How do I automate the backup process on a remote server?
To automate the backup process on a remote server, you can use the cron tool. Create a bash script for the backup, and then create a cron job that will execute this script at a specified time.

Definitions:
Linux: An open-source operating system that offers greater flexibility than proprietary software operating systems.
rsync: A program used for file synchronization and backup.
configuration: The process of setting certain parameters or options to specify the behavior of a program or system.
backup: A copy of data created for the purpose of recovering it in case of loss of the original data.
bash script: A script written in the Bash language that performs specific tasks or operations.
cron tool: A tool in Linux used for scheduling and executing specific tasks at specified times.

Links:
linux.org – Linux.org homepage
rsync.samba.org – Official rsync program website

The source of the article is from the blog lanoticiadigital.com.ar