Link to home
Start Free TrialLog in
Avatar of willlandymore
willlandymoreFlag for United States of America

asked on

rsync transfer logging and leaving certain folders after sync....

I have rsync running and I'm using the --delete-source-files in the script so after the transfer is done it gets rid of the files in the source folder. However, if I wanted to get rid of everything but then leave say 'folder1' and 'folder2' in there permanently, how do I make exceptions like that with the delete?
Also, if I want to turn on transfer logging so it shows what files were sent across successfully and then write it to a file, is that possible?
Avatar of willlandymore
willlandymore
Flag of United States of America image

ASKER

Okay, I found the one for the logs...

--log-file=/log/rsyncTransfer.log

when I add this to the command I get the log with the transfer stats....

Just can't find a way to delete everything from the source directory (including folders) but leave a couple of folders there permanently.
> --delete-source-files
Should it be --remove-source-files?
That's the new option for rsync version 3.x.
This option will not remove the directories, just those files are successfully transferred.

> turn on transfer logging so it shows what files were sent across successfully and then write it to a file
Use --log-file, i.e.
--log-file=/tmp/rsync.log

> leave say 'folder1' and 'folder2' in there permanently,
--exclude=PATTERN
is based on the files matching PATTERN, not on directory.
yeah, I meant 'remove' but typed delete. Is there a way to get rid of the directories too through the same command so once rsync is finished successfully then it will remove the directories too?
I've been through every forum and man page and I can't see a way to get rid of the directories...but it seems kind of silly to not have that option if you have them all for the files.

I did find this thread but I couldn't figure out how they were running that in with the rsync command...
You can remove empty dir after rsync

for DIR in `find /path-to-rsync-top-dir -type -d | sort -r`;
do
   /bin/rmdir --ignore-fail-on-non-empty $DIR
done
but then I would have no way of knowing if the sync has finished so I might be trying to delete the folders before it's through....that's the only reason I'm trying to put it in the rsync command itself.

The -remove-source-files is so handy because it does the transfer and then as soon as it's through it gets rid of the files....just need that to happen with the folders. :)
> no way of knowing if the sync has finished so
rmdir only work for empty directory. If there are still files in that directory, rmdir will fail.
 option --ignore-fail-on-non-empty is to ignore the error message, it will not remove the non-empty directory.

If the file fail to transfer, then --remove-source-files will not remove the files. So rmdir can not remove the directory nor the files.
right now I have the bash file running:

rsync -rz --remove-source-files --log-file=/var/log/rsynctest.log "/path1/" "/path2/"

is there any way to run what you are suggesting after that? I've tried putting it in after but it will only run the rsync command and then stop. Sorry....still learning linux. :)
Create a script by using any editor
--- ~/rsync.sh  ---
#!/bin/bash

/usr/bin/rsync -raz --remove-source-files --log-file=/var/log/rsynctest.log "/path1/" "/path2/"

for DIR in `find /path1 -type -d | sort -r`;
do
   /bin/rmdir --ignore-fail-on-non-empty $DIR
done
---------------
Then
chmod +x ~/rsync.sh

You can execute
~/rsync.sh
I think that might work but there are white spaces in the path so it didn't like it. I tried using "", \, \040...any escape character I could think of, but it has a real problem with those space...
> there are white spaces in the path

Try this one
---------------
#!/bin/bash

/usr/bin/rsync -raz --remove-source-files --log-file=/var/log/rsynctest.log "/path1/" "/path2/"

find /path1 -type -d | sort -r > /tmp/dir_tmp.txt

while read DIR
do
   /bin/rmdir --ignore-fail-on-non-empty "$DIR"
done < /tmp/dir_tmp.txt

/bin/rm -f  /tmp/dir_tmp.txt
When I used that and double-quoted the path it was okay, however, it removed the top level directory instead of all the empty folders under it....

I had something like /usr/rsync/transfer/

and then there was Testfolder in the 'transfer' one but it removed transfer and Testfolder....
ASKER CERTIFIED SOLUTION
Avatar of wesly_chen
wesly_chen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the help.