-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
1 changed file
with
18 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,20 +12,20 @@ tags: | |
This is everything you will need to understand and follow along with this guide: | ||
|
||
- A computer running Rocky Linux | ||
- To be comfortable with modifying configuration files from the command-line | ||
- To be comfortable with modifying configuration files from the command line | ||
- Knowledge of how to use a command line editor (using _vi_ here, but you can use your favorite editor) | ||
- You will need root access or `sudo` privileges | ||
- Public and Private SSH key pairs | ||
- Able to create a bash script with `vi` or your favorite editor, and test it. | ||
- Able to create a bash script with `vi` or your favorite editor and test it. | ||
- Able to use `crontab` to automate the running of the script | ||
|
||
## Introduction | ||
|
||
Using `rsync` over SSH is not as powerful as [lsyncd](../backup/mirroring_lsyncd.md) (which allows you to watch a directory or file for changes and keep it synchronized in real time), nor as flexible as [rsnapshot](../backup/rsnapshot_backup.md) (which offers the ability to backup multiple targets from a single computer). However, it provides the ability to keep two computers up to date on a schedule that you define. | ||
Using `rsync` over SSH is not as powerful as [lsyncd](../backup/mirroring_lsyncd.md) (which allows you to watch a directory or file for changes and keep it synchronized in real-time), nor as flexible as [rsnapshot](../backup/rsnapshot_backup.md) (which offers the ability to backup multiple targets from a single computer). However, it allows you to keep two computers up to date on a schedule you define. | ||
|
||
If you need to keep a set of directories on the target computer up to date, and you do not care about real-time synchronization as a feature, then `rsync` over SSH is probably the best solution. | ||
|
||
For this procedure, you will be doing things as the root user. Either login as root or use the `sudo -s` command to switch to the root user in your terminal. | ||
For this procedure, you will be working as the root user. Either log in as root or use the `sudo -s` command to switch to the root user in your terminal. | ||
|
||
### Installing `rsync` | ||
|
||
|
@@ -35,15 +35,15 @@ Although `rsync` is probably already installed. To ensure that `rsync` is up to | |
dnf install rsync | ||
``` | ||
|
||
If the package is not installed, `dnf` will ask you to confirm the installation, if it is already installed, `dnf` will look for an update and prompt to install it. | ||
If the package is not installed, `dnf` will ask you to confirm the installation. If it is already installed, `dnf` will look for an update and prompt you to install it. | ||
|
||
### Preparing the environment | ||
|
||
This example will use `rsync` on the target computer to pull from the source instead of pushing from the source to the target. You need to set up an [SSH key pair](../security/ssh_public_private_keys.md) for this. After creating the SSH key pair, verify passwordless access from the target computer to the source computer. | ||
|
||
### `rsync` parameters and setting up a script | ||
|
||
Before script set up continues, you need to decide what parameters you want to use with `rsync`. Many possibilities exist. Review the [manual for rsync](https://linux.die.net/man/1/rsync) for a complete list. The most common way to use `rsync` is to use the `-a` option, because `-a`, or "archive", combines several common options into one. What does `-a` include? | ||
Before the script setup continues, you must decide what parameters to use with `rsync`. Many possibilities exist. Review the [manual for rsync](https://linux.die.net/man/1/rsync) for a complete list. The most common way to use `rsync` is to use the `-a` option, because `-a`, or "archive", combines several common options. What does `-a` include? | ||
|
||
- `-r`, recurse the directories | ||
- `-l`, maintains symbolic links as symbolic links | ||
|
@@ -58,7 +58,7 @@ The only other options that you need to specify in this example are: | |
- `-e`, specify the remote shell to use | ||
- `--delete`, which says if the target directory has a file in it that does not exist on the source, get rid of it | ||
|
||
Next, set up a script on the target computer by creating a file for it (again, use your favorite editor if you are not familiar with `vi`). To create the file, just use this command: | ||
Next, set up a script on the target computer by creating a file for it (again, use your favorite editor if you are unfamiliar with `vi`). To create the file, use this command: | ||
|
||
```bash | ||
vi /usr/local/sbin/rsync_dirs | ||
|
@@ -71,7 +71,7 @@ Add the content: | |
/usr/bin/rsync -ae ssh --delete [email protected]:/home/your_user /home | ||
``` | ||
|
||
Replace "source.domain.com" with your own domain name, host name, or IP address. | ||
Replace "source.domain.com" with your domain name, hostname, or IP address. | ||
|
||
Make it executable: | ||
|
||
|
@@ -93,15 +93,15 @@ Run the script: | |
/usr/local/sbin/rsync_dirs | ||
``` | ||
|
||
If all is well, you will get a completely synchronized copy of your home directory on the target computer. Ensure this is the case. | ||
If all is well, you will get a synchronized copy of your home directory on the target computer. Ensure this is the case. | ||
|
||
Assuming all of that worked, create a new file on the source computer in your home directory: | ||
|
||
```bash | ||
touch /home/your_user/testfile.txt | ||
``` | ||
|
||
Run the script again: | ||
Rerun the script: | ||
|
||
```bash | ||
/usr/local/sbin/rsync_dirs | ||
|
@@ -113,7 +113,7 @@ Verify that the target computer receives the new file. If so, the next step is t | |
rm -f /home/your_user/testfile.txt | ||
``` | ||
|
||
Run the script again: | ||
Rerun the script: | ||
|
||
```bash | ||
/usr/local/sbin/rsync_dirs | ||
|
@@ -133,13 +133,13 @@ Run the script a final time: | |
/usr/local/sbin/rsync_dirs | ||
``` | ||
|
||
The file you created on the target should no-longer exist, because it does not exist on the source. | ||
The file you created on the target should no longer exist because it does not exist on the source. | ||
|
||
Assuming all of this worked as expected, change the script to synchronize all the directories that you want. | ||
Assuming all of this worked as expected, change the script to synchronize all the directories you want. | ||
|
||
## Automating everything | ||
|
||
You probably do not want to manually run this script every time you want to synchronize. Use a `crontab` to do this automatically on a schedule. To run this script at 11 PM every night: | ||
You may not want to run this script every time you want to synchronize manually. Use a `crontab` to do this automatically on a schedule. To run this script at 11 PM every night: | ||
|
||
```bash | ||
crontab -e | ||
|
@@ -175,15 +175,15 @@ This will pull up and look similar to this: | |
|
||
!!! info | ||
|
||
The example `crontab` shows an empty, but commented, file. The commenting does not show up on every computer instance and may be an empty file. On an active computer, you may see other entries. | ||
The example `crontab` shows an empty but commented file. The commenting does not appear on every computer instance and may be an empty file. On an active computer, you may see other entries. | ||
|
||
The `crontab` is on a 24-hour clock. You will need your entry at the bottom of this file is: | ||
The `crontab` is on a 24-hour clock. You will need your entry at the bottom of this file: | ||
|
||
```crontab | ||
00 23 * * * /usr/local/sbin/rsync_dirs | ||
``` | ||
|
||
What this says is to run this command at 00 minute, 23 h, every day, every month, and every day of the week. Save your `crontab` entry with: | ||
What this says is to run this command at 00 minutes, 23 h, every day, every month, and every day of the week. Save your `crontab` entry with: | ||
|
||
++shift+colon+"w"+"q"+exclam++ | ||
|
||
|
@@ -198,4 +198,4 @@ What this says is to run this command at 00 minute, 23 h, every day, every month | |
## Conclusions | ||
Although `rsync` is not as flexible or powerful as other tools, it provides file synchronization, which is always useful. | ||
Although `rsync` is not as flexible or robust as other tools, it provides file synchronization, which is always helpful. |