File synchronization and transfer between directories or across networked systems is a common task in IT. One of the most powerful and versatile tools for this purpose is rsync
. This article explains various rsync
commands with examples and explanations of the command prompt instructions.
Installing rsync
First, ensure rsync
is installed on your system. On Ubuntu or other Debian-based systems, you can install it using the following command:
sudo apt-get install rsync
This command uses apt-get
, the package handling utility in Debian-based distributions, with sudo
to execute the command as a superuser, and installs the rsync
package.
Basic rsync Usage
Synchronizing Directories
The basic syntax of rsync
is as follows:
rsync /app/ /targetfolder
This command synchronizes the contents of /app/
with /targetfolder
. It copies all files and directories from the source (/app/
) to the destination (/targetfolder
).
Common Options
-v
: Verbose mode, provides detailed information about the transfer process.-r
: Recursively transfer directories and their contents.-a
: Archive mode, which preserves permissions, times, symbolic links, and other attributes.-z
: Compress file data during the transfer.
Examples with Options
- Verbose and Compressed Transfer
rsync -vrz /app/ /targetfolder
This command adds verbosity (-v
), recursion (-r
), and compression (-z
), making the transfer more efficient and informative.
- Archive Mode
rsync -a /app/ /targetfolder
Using -a
, this command ensures that all file attributes are preserved during the transfer.
- Dry Run
rsync -anv /app/ /targetfolder
The -n
option performs a dry run, showing what would be transferred without actually copying any files.
- Archiving with Compression
rsync -avz /app/ /targetfolder
This command combines archive mode (-a
), verbosity (-v
), and compression (-z
), providing a comprehensive synchronization process.
- Running as Superuser
sudo rsync -avz /app/ /targetfolder
Using sudo
allows the command to run with superuser privileges, useful when accessing files that require elevated permissions.
Remote Synchronization
- Pushing Data to a Remote Server
rsync -avz /app/ vagrant@192.168.33.10:~/backup/
This command synchronizes /app/
to the ~/backup/
directory on a remote server with IP address 192.168.33.10
using the vagrant
user.
- Specifying Remote Directory
rsync -avz /app/ vagrant@192.168.33.10:/home/backup/
This command pushes /app/
to /home/backup/
on the remote server.
- Pulling Data from a Remote Server
rsync -avz vagrant@192.168.33.10:/home/backup/ /app/
This command pulls data from the remote server’s /home/backup/
directory to the local /app/
directory.
Scheduling with Cron
To automate synchronization, you can schedule rsync
with cron. For example, to run the synchronization every day at 2 AM:
0 2 * * * rsync -avz /app/ vagrant@192.168.33.10:/home/backup/
Using SSH and Custom Ports
To use rsync
over SSH with a custom port:
rsync -avz -e "ssh -p 2222" /app/ vagrant@192.168.33.10:/home/backup/
The -e
option specifies the remote shell program to use, in this case, SSH with port 2222
.
Deleting and Existing Files
- Delete Files in Destination Not Present in Source
rsync --delete -avz /app/ vagrant@192.168.33.10:/home/backup/
- Update Only Existing Files
rsync --existing -avz /app/ vagrant@192.168.33.10:/home/backup/
Including and Excluding Files
- Including Specific Files
rsync -avzi /app/ vagrant@192.168.33.10:/home/backup/
The -i
option provides a detailed list of changes for each file.
- Including and Excluding File Patterns
rsync --include '*.php' --exclude '*.txt' -avz /app/ vagrant@192.168.33.10:/home/backup/
This command includes files ending with .php
and excludes those ending with .txt
.
Conclusion
rsync
is a versatile and efficient tool for synchronizing files and directories both locally and remotely. By understanding and using its various options, IT professionals can ensure reliable and efficient data transfer and backup processes.