Copying files to or from OpenShift Container Platform containers¶
You can use the oc rsync command, or remote sync, to copy local files to or from a remote directory in a OpenShift Container Platform container.
The oc rsync command leverages the local Rsync tool on the client machine and the remote container for tasks such as copying database archives to and from your pods for backup and restore purposes, or copying source code changes into a running pod for development debugging.
Note
If the Rsync tool is not found locally or in the remote container, a tar archive is created locally and sent to the container where the tar utility is used to extract the files. If tar is not available in the remote container, the copy fails.
Understanding how to copy files¶
You can use the oc rsync command, or remote sync, for copying database archives to and from your pods for backup and restore purposes. You can also use oc rsync to copy source code changes into a running pod for development debugging, when the running pod supports hot reload of source files.
When using the oc rysnc command, note the following requirements.
- Specifying the Copy Source
-
The source argument of the
oc rsynccommand must point to either a local directory or a pod directory. Individual files are not supported.When specifying a pod directory, the directory name must be prefixed with the pod name:
If the directory name ends in a path separator (
/), only the contents of the directory are copied to the destination. Otherwise, the directory and its contents are copied to the destination. - Specifying the Copy Destination
- The destination argument of the
oc rsynccommand must point to a directory. If the directory does not exist, butrsyncis used for copy, the directory is created for you. - Deleting Files at the Destination
- The
--deleteflag can be used to delete any files in the remote directory that are not in the local directory. - Continuous Syncing on File Change
-
Using the
--watchoption causes the command to monitor the source path for any file system changes, and synchronizes changes when they occur. With this argument, the command runs continuously.Synchronization occurs after short quiet periods to ensure a rapidly changing file system does not result in continuous synchronization calls.
When using the
--watchoption, the behavior is effectively the same as manually invokingoc rsyncrepeatedly, including any arguments normally passed tooc rsync. Therefore, you can control the behavior via the same flags used with manual invocations ofoc rsync, such as--delete.
Copying files to and from containers¶
You can use the oc rsync command for copying local files to or from a container.
Prerequisites
-
rsync must be installed. The
oc rsynccommand uses the localrsynctool, if present on the client machine and the remote container.If
rsyncis not found locally or in the remote container, a tar archive is created locally and sent to the container where the tar utility is used to extract the files. If tar is not available in the remote container, the copy will fail.The tar copy method does not provide the same functionality as
oc rsync. For example,oc rsynccreates the destination directory if it does not exist and only sends files that are different between the source and the destination.Note
In Windows, the
cwRsyncclient should be installed and added to the PATH for use with theoc rsynccommand.
Procedure
-
To copy a local directory to a pod directory:
For example:
-
To copy a pod directory to a local directory:
Using advanced Rsync features¶
You can use the additional command-line options that are available with the standard rsync command if needed.
The oc rsync command exposes fewer command-line options than standard rsync command. In the case that you want to use a standard rsync command-line option that is not available with the oc rsync command, for example the --exclude-from=FILE option, it might be possible to use standard rsync with the --rsh (-e) option or RSYNC_RSH environment variable as a workaround, as shown in the following examples:
or:
Export the RSYNC_RSH variable:
Then, run the rsync command:
Both of the above examples configure the standard rsync command to use oc rsh as its remote shell program to enable it to connect to the remote pod, and are an alternative to running oc rsync.