Use the -backup and -backup-dir options to enable backups. You can create a backup of these files before deleting them. You can override this behavior with a -delete flag. $ rsync -azP -exclude-from 'exclude-list.txt' source/ destination/īy default, Rsync does not delete destination files, even if they are deleted from the source location. $ rsync -azP -exclude 'dir*' source/ destination/Įxclude a list of patterns or file names: Similarly, if you need to include some specific files only, you can specify the inclusion pattern with options -include=pattern or -include-from=filename.Įxclude a single file or files matching with a single pattern: If you need to specify multiple exclusion patterns, then you can specify all such patterns in a text file and include that file in command with the options -exclude-from=filename. ![]() Rsync -azP -e "ssh -p port_number" source destinationĪnther common option is -exclude, which specifies the pattern for file names to be excluded. If your remote SSH server runs on a port other than 22, then you can use a slightly modified version of the preceding command as follows: Note that the SSH connection is established on the default SSH port, port 22. You can skip the -e flag and rsync will use a non-encrypted connection to transfer data and login credentials. If you have not set public key authentication between two servers, you will be asked to enter a password for your account on the remote server. In the preceding command, we are using SSH with public key authentication. We have used one more option, -e, which specifies which remote shell to be used while downloading files. Option -z is used to enable compression while transferring files, while option -P enables progress reports and the resumption of interrupted downloads by saving partial files. This option enables recursive synchronization and preserves modification time, symbolic links, users, and group permissions. Some options that are used in the preceding example are –a, a combination of various other flags and stands for achieve. $ rsync -azP -e ssh backup provides tons of command line options. Use the following command to push files to the remote server: Rsync can be used to upload files as well. In the preceding example, we have used a pull operation, where we are downloading files from the remote server. $ crontab -e # open crontab rsync -aze ssh /var/backup Open a cron jobs file with ctontab-e and add the following line to enable daily backups: ![]() You can quickly schedule a daily backup with a cron and Rsync. The advantage of using Rsync is that file synchronization happens incrementally, that is, only new and modified files will be downloaded. This tool is commonly used as a simple backup utility to copy or move files around systems. With Rsync, you can synchronize files between two local directories, as well as files between two servers. Rsync is a well known command line file synchronization utility. $ rsync /var/log/mysql ~/mysql_log_backup To synchronize two local directories, you can simply specify the source and destination path with rsync, as follows: The output should look similar to the following rsync -azP -e ssh backup ![]() This time, rsync will only download a new file and any other update files. Now re-execute the rsync command on the destination server. You can check the downloaded files with the ls command:Īdd one new file on the remote server under touch sampledir/file22 The output of the command should look like the following screenshot: Without /, the entire sampledir will be copied to the rsync -azP -e ssh backupĪs this is the first time, all files from sampledir on the remote server will be downloaded in a backup directory on your local system. This will copy contents of sampledir in the backup. Now, use the following command to synchronize the entire directory from the source server to your local system. You can use existing files as mkdir touch sampledir/file ![]() We can use password authentication as well, which is described later in this recipe.Ĭreate a sample directory structure on the source server. Set up key-based authentication between source and destination servers. In this recipe, we will learn how to use the Rsync utility to synchronize files between two directories or between two servers.įollow these steps to synchronize files with Rsync:
0 Comments
Leave a Reply. |