GoAdmio

cf

Copy the contents of a folder to another folder on this server, or to a remote server, with rsync

On this page: Overview · Quick start · Syntax · Options · Output · Examples · Troubleshooting · Details · See also


Overview

/go/cf ("copy files") copies everything inside a source folder into a destination folder. It uses rsync, so:

The destination can be another folder on this server, or a folder on a remote server.


Quick start

# Copy a folder's contents into another folder on this server
/go/cf /home/example.com/www /backup/example.com

# Copy a folder's contents to a remote server
/go/cf /home/example.com/www /home/example.com/www 203.0.113.10 'RemoteRootPassword'

Syntax

/go/cf <from> <to>
/go/cf <from> <to> <remote-server> <remote-password>
Argument Description
<from> The source folder on this server. It must exist.
<to> The destination folder. On this server, or on the remote server.
<remote-server> The IP address or host name of the remote server.
<remote-password> The root password of the remote server.

A trailing / on either folder is added if you leave it out. The contents of <from> are copied into <to>, not the <from> folder itself.


Options

Form Arguments What it does
Local copy 2 Copies the contents of <from> into <to> on this server. Both folders must already exist.
Remote copy Exactly 4 Copies the contents of <from> into <to> on the remote server, as root, over SSH.

Rules

Item Rule
Paths for a local copy Both must contain a /. Otherwise the command does nothing.
Remote copy arguments Exactly four. The SSH port is always 22 (see Known issues).
Deleted files Files removed from <from> are not removed from <to>.
Spaces in paths Not supported.

Output

Message Meaning
Transferring files from <from>/ to <to>/... A remote copy started.
<folder> Folder not exists. The source folder (or, for a local copy, the destination) does not exist.

Neither form prints a list of files or a final message. A local copy is completely silent unless rsync reports an error. Each run adds a line to /var/log/go/cf.log (see Security).


Examples

# Local copy
/go/cf /home/example.com/www /backup/example.com
# Remote copy
/go/cf /var/skeleton /var/skeleton 203.0.113.10 'RemoteRootPassword'
# Check the result on the remote server
ssh root@203.0.113.10 'ls -la /var/skeleton'

Troubleshooting

Symptom Likely cause Fix
<folder> Folder not exists. The source folder is missing. For a local copy, the destination folder is missing. Check the paths, and create the destination folder for a local copy.
Nothing happens and nothing is printed The paths have no /, or the number of arguments is not 2 or 4. Use full paths, and check the argument count.
The remote copy fails with mkdir ... failed: No such file or directory The remote parent folder does not exist. The script does not create it (see Known issues). Create the folder on the remote server first.
mkdir: invalid option -- 'P' A mistake in the script's folder-creation step. Create the folder yourself. The copy can still continue.
Permission denied Wrong remote password, or root password login is off on the remote server. See permitrootlogin.
Using a different SSH port does nothing, or copies locally A fifth argument changes the form of the command. See Known issues.
Files deleted at the source are still at the destination rsync is run without --delete. Delete them at the destination yourself.
Files are not readable or owners are wrong after a local copy The local copy does not use sudo. Run the command as root.

Details

How it works

  1. Logs the run to /var/log/go/cf.log.
  2. With exactly 4 arguments, runs the remote copy:
    1. Checks the source folder, and adds a trailing / to both folders.
    2. Tries to create the destination folder on the remote server.
    3. Prints Transferring files from ... to ....
    4. Runs rsync -azlh through sshpass and ssh on port 22, with host key checking turned off.
  3. Otherwise, if both of the first two arguments contain a /, runs the local copy: checks that both folders exist, adds trailing slashes, and runs rsync -azlh.

rsync flags

Flag Meaning
-a Archive: keep permissions, owners, groups, timestamps, and links.
-z Compress during transfer.
-l Copy symbolic links as links.
-h Human-readable numbers.

Security

[!WARNING]

  • The whole command line is written to /var/log/go/cf.log, so the remote password is saved there in plain text. Remove the line after a remote copy.
  • The password on the command line can be seen in the process list and is saved in your shell history.
  • SSH host key checking is turned off and known hosts are not saved, so the identity of the remote server is not verified.

Known issues


See also

Command Description
fsync Copy a directory from your local server to a remote server
copywww Copy the www directory from one domain to another
copy Copy a GoAdmio site from one server to another
cb Copy GoAdmio backups from the local server to a remote server
serversync Back up and migrate a GoAdmio server with all configurations
permitrootlogin Allow root SSH login from a specific IP