Avatar of willlandymore
willlandymore
Flag 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?
LinuxLinux Networking

Avatar of undefined
Last Comment
willlandymore

8/22/2022 - Mon
willlandymore

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.
wesly_chen

> --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.
willlandymore

ASKER
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?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
willlandymore

ASKER
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...
wesly_chen

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
willlandymore

ASKER
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. :)
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
wesly_chen

> 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.
willlandymore

ASKER
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. :)
wesly_chen

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 started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
willlandymore

ASKER
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...
wesly_chen

> 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
willlandymore

ASKER
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....
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
wesly_chen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
willlandymore

ASKER
Thanks for the help.