Efficient File Synchronization with rsync
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
Examples with Options
rsync -vrz /app/ /targetfolder
This command adds verbosity (-v), recursion (-r), and compression (-z), making the transfer more efficient and informative.
rsync -a /app/ /targetfolder
Using -a, this command ensures that all file attributes are preserved during the transfer.
rsync -anv /app/ /targetfolder
The -n option performs a dry run, showing what would be transferred without actually copying any files.
rsync -avz /app/ /targetfolder
This command combines archive mode (-a), verbosity (-v), and compression (-z), providing a comprehensive synchronization process.
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
领英推荐
rsync -avz /app/ [email protected]:~/backup/
This command synchronizes /app/ to the ~/backup/ directory on a remote server with IP address 192.168.33.10 using the vagrant user.
rsync -avz /app/ [email protected]:/home/backup/
This command pushes /app/ to /home/backup/ on the remote server.
rsync -avz [email protected]:/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/ [email protected]:/home/backup/
Using SSH and Custom Ports
To use rsync over SSH with a custom port:
rsync -avz -e "ssh -p 2222" /app/ [email protected]:/home/backup/
The -e option specifies the remote shell program to use, in this case, SSH with port 2222.
Deleting and Existing Files
rsync --delete -avz /app/ [email protected]:/home/backup/
rsync --existing -avz /app/ [email protected]:/home/backup/
Including and Excluding Files
rsync -avzi /app/ [email protected]:/home/backup/
The -i option provides a detailed list of changes for each file.
rsync --include '*.php' --exclude '*.txt' -avz /app/ [email protected]:/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.