You can use the Rsync tool to create backups.
You can copy data locally to another directory or drive on the system, or to a remote system.
Note: Depending on the amount of data, the first run may take significantly longer than subsequent runs. This is because Rsync synchronizes all data the first time; from the second run onward, only changed data is transferred. This creates an incremental backup.
You can install Rsync with the following command:
apt install rsync
Once installed, you can use it right away.
In this example, the Client folder in /home is synchronized with the Backup folder in /home.
Use the following command:
rsync -arz /home/Client /home/Backup
-a=Archive mode; copies all attributes
-r=Recursive; also synchronizes subfolders
-z=Compression; data is compressed depending on the amount and size of the data
The folder was successfully synchronized.
If a file, etc. is deleted from the Client folder, it remains in the Backup folder.
However, if the files should always be identical, you can modify the rsync command so that data no longer present in the Client folder is also removed from the Backup folder.
The modified command is:
rsync -arz --delete /home/Client /home/Backup
-a=Archive mode; copies all attributes
-r=Recursive; also synchronizes subfolders
-z=Compression; data is compressed depending on the amount and size of the data
--delete= Deletes data that no longer exists in the source but is still present in the destination
To avoid running the command manually every time, you can add it to a cron job.
For example, to create a backup every day at 3 a.m.:
Open crontab -e:
Choose option 1 to use "nano" as the editor. Choose option 2 to use "vim" as the editor.
After opening the file, for example with Nano, you can create and add a cron job.
You can create a cron job with this generator.
The cron job will look like this:
0 3 * * * rsync --progress -arz --delete -e "ssh -i /home/sshkey/keybackup" /home/Client/ root@123.123.123.123:/home/Backup/Home-Server1/ >/dev/null 2>&1
The command runs every day at 3 a.m. and creates a backup.
In this example, the Client folder in /home is synchronized with the Backup folder on a remote server. The connection uses an SSH key so that the backup can run automatically.
Important: Rsync must also be installed on the remote server.
Codeapt install rsync
For example, you can use the following command to back up the data to the remote host (adjust as needed):
rsync --progress -arz -e "ssh -i /home/sshkey/keybackup" /home/Client/ root@123.123.123.123:/home/Backup/Home-Server1/
-a=Archive mode; copies all attributes
-r=Recursive; also synchronizes subfolders
-z=Compression; data is compressed depending on the amount and size of the data
-e=Specifies the remote shell to use (default: ssh), for example, to specify an SSH key or a different port
Specify the SSH key (path)("ssh -i /home/sshkey/keybackup")= ssh -i /[path to key]
Specify the directory to back up (/home/client/)= /[directory]
Remote host (root@123.123.123.123:)= User login name on the remote host and address: name@IP/domain
Destination directory on the remote host (:/home/Backup/Home-Server1/)= :/[path to destination directory]
After running the command, the folders/files were successfully synchronized/backed up to the remote directory.
If a file, etc. is deleted from the Client folder, it remains in the Backup folder on the remote host.
However, if the files should always be identical, you can modify the rsync command so that data no longer present in the Client folder is also removed from the Backup folder on the remote host.
The modified command is:
rsync --progress -arz --delete -e "ssh -i /home/sshkey/keybackup" /home/Client/ root@123.123.123.123:/home/Backup/Home-Server1/
-a=Archive mode; copies all attributes
-r=Recursive; also synchronizes subfolders
-z=Compression; data is compressed depending on the amount and size of the data
--delete= Deletes data that no longer exists in the source but is still present in the destination
-e=Specifies the remote shell to use (default: ssh), for example, to specify an SSH key or a different port
Specify the SSH key (path)("ssh -i /home/sshkey/keybackup")= ssh -i /[path to key]
Specify the directory to back up (/home/client/)= /[directory]
Remote host (root@123.123.123.123:)= User login name on the remote host and address: name@IP/domain
Destination directory on the remote host (:/home/Backup/Home-Server1/)= :/[path to destination directory]
To avoid running the command manually every time, you can add it to a cron job.
For example, to create a backup every day at 3 a.m.:
Open crontab -e:
Choose option 1 to use "nano" as the editor. Choose option 2 to use "vim" as the editor.
After opening the file, for example with Nano, you can create and add a cron job.
You can create a cron job with this generator.
The cron job will look like this:
0 3 * * * rsync --progress -arz --delete -e "ssh -i /home/sshkey/keybackup" /home/Client/ root@123.123.123.123:/home/Backup/Home-Server1/ >/dev/null 2>&1
The command runs every day at 3 a.m. and creates a backup.