How to delete source files, copied using scp?

From FVue
Jump to: navigation, search

Problem

I want to copy Apache log files from a remote machine and delete them afterwards – if copying was successful.

Solution 1: rsync --remove-source-files

Use rsync over ssh and tell it to remove synchronized files (non-dir):

rsync -avz --remove-source-files server:log/yyyymmdd.gz myfile

Thanks to tc0nn.

Solution 2: scp && ssh rm

After a successful scp, ssh to server with rm command:

scp server:foo . && ssh server rm foo

Workaround

After having copied the source file, copy an empty source file back to the original source file. This frees space, but leaves a filename on the server though.

# Copy is successful?
if scp server:log/yyyymmdd.gz myfile; then
    # Yes, copy is successful;
    # Create empty file
    touch empty_file
    # Copy empty file to server source file
    scp empty_file server:log/yyyymmdd.gz
fi

Journal

Someone submitted (and withdrew) an scp patch to delete source files after copy.

Perhaps I could schedule the copy on the client.

Comments

blog comments powered by Disqus