Script

Hello there,
Am trying to copy all the folders and their content (files) created on yesterday to a remote machine in LAN, without changing the folder structure and name.
The script i build is not working properly..
Can you please guide me to achieve.
Here goes the script which i wrote..

#!/bin/bash
$source = \path\to\source\
$target = \\10.xx.xx.x\path\to\the\directory\
find .$source/ -type -d -mtime -1  -exec rsync -auvrp \{\} $target \;

Thanks in advance.

@vijaydsk,
please start using markdown code tags when posting data/code sample. I've edited your OP already.

As there's multiple issues with your sample code, I'd start by "validating" your script at shell check first, fixing the identified issues and go from there.

Just corrected the code..

#!/bin/bash
source='/path/to/source/'
target='//10.xx.xx.x/path/to/the/directory/'
find ."$source"/ -type -d -mtime -1 -mtime +1 -exec rsync -auvrp \{\} "$target" \;

Let me know the above code copies all the folders created yesterday and copies to the destination in LAN?

1 Like

I think you want
-type d -mtime 1
This addresses mtimes >24hours and <48 hours.
Because rsysnc is recursive you can at that point prune the recursion in find:
-type d -mtime 1 -prune -exec ...

1 Like

Thanks @MadeInGermany
So after modification my code is as follows...

#!/bin/bash
source='/path/to/source/'
target='//10.xx.xx.x/path/to/the/directory/'
find ."$source"/ -type -d -mtime 1 -prune -exec rsync -auvrp \{\} "$target" \;

Can you please let me know what exactly prune does?
And another small help...
If I need to copy files created 120 days, mtime 120 is sufficient? or do I need to change anything?
Thanks in advance.

Do you want to copy all the files created within the last 120 days, or just the ones from exactly that day 120 days ago?

Just the ones created exactly on 120th day..

The -prune skips the recursion into further sub directories.
It should be used if there is already a recursive tool like rsync.
The -type d -mtime 120 examines the date of the directory.

If you want to examine the date of the individual files then it is
-type f -mtime 120
and rsync runs on the files so won't do a recursion so there should not be -prune

1 Like

Working with

exactly 120 days

can turn into an issue. If a day is skipped (machine down, unexpected failure, target file system full), you won't get it back.

Minimum suggestion is to have a default of 120 days, but an option to specify another number. It would also be helpful to log all actions, or to have a reporting script to detect omissions.

My style is to copy all files older than 120 days which do not already have a copy. Rsync should take care of that last part in any case.

Also, -mtime does not select days. It selects intervals of 86400 seconds from the time you run it, so if you run at 11:30:00 you will get files from 11:30:00 on one day to 11:29:59 on the next day. In GNU find, the -daystart option is frequently helpful.

1 Like

Thanks @Paul_Pedant for the good idea.
Can you help me modifying my code?
My requirement is as follows..
All the directories (with files) to be copied on the an external disk connected into another ubuntu machine and remove from the server.
On average 600 to 700 folders will be created and each folder is having .png files in it.
I need to maintain 120 days data in the server and rest move out to external hdd.

Hi @vijaydsk

You need to try writing your own code first, making a "real attempt" before simply asking others to continually rèwrite code for you here.

Is this clear?

Provide your work based on the guidance you have already received my our team members.

Hello @Neo
Agree.
If you look from the other (my) angle, am asking the experts to modify my code which I wrote with mistakes in my first post.
Hence, I provided my requirement.
Hope am clear in asking.
For me, forums are to share the knowledge and help others to learn and improve.
Expected the same from this community too.
Correct me if am wrong.

You are wasting time not posting code like I asked and posting philosophy instead :slight_smile:

Talking and chatting is not the same as coding and trying to code.

Just look at your topic title:

Script

Even your topic title is mostly meaningless and shows little -to-no thought process nor effort.

You must try. You must code. That is how to learn. Just do it and then ask questions; but please do not ask others to do your work for you.

Thank you.

See also: