• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 360
  • Last Modified:

Copying files with a script

Hi Experts,

I have log files generating every 12 hours a day. I need to copy them with a script everyday  to my home directory.

The logs files are in this format:
 loga_12.09.18_00.00.00.log
 loga_12.09.18_12.00.00.log
 loga_12.09.19_00.00.00.log
 loga_12.09.19_12.00.00.log
 loga_12.09.20_00.00.00.log
0
07592161981m
Asked:
07592161981m
1 Solution
 
savoneCommented:
let's say the logs are in /var/log/


Here is the script, create a new file called move_logs.sh with the following contents.

cd /var/log/
cp loga* /home/username/

Then execute it with a cronjob by running the command "crontab -e" and add the following:

59 23 * * * /path/to/move_logs.sh


if the logs are protected and only run can see them you will have to do all above as root.  Also you may want to change the permissions so your user can view them.  In that case use this:

cd /var/log/
cd loga* /home/username/
chown username /home/username/loga*

If you give more information like where the logs are located, etc... I can give you the exact script.  Also if these logs are being written to constantly you might want to stop the service writing to them or only move the logs that are already rotated.
0
 
07592161981mAuthor Commented:
If I use loga*, then all the files will repeatedly copied to the destination directory.
I  need to copy only two files today that were generated yesterday.

 loga_12.09.19_12.00.00.log
 loga_12.09.20_00.00.00.log.
0
 
mrandalCommented:
I often use the sfk tool for this, which has flexible file selection. If you strictly want to copy only files changed about 1 day ago, try
sfk select -since 2d -before 1d mylogs loga +run "cp #qfile /home/username" -yes

Open in new window

which selects all files *loga* from mylogs, newer than 2 days, but older than 1 day, and runs a cp command on them.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
savoneCommented:
Easier...

Here is your script, change as needed.


find /var/log/ -mtime -1 -name "loga*" -exec mv {} /home/username/ \;

This will find any file in /var/log that starts with loga that has been modified in the last 24 hours and move it to the specified directory, in this case /home/username/
0
 
savoneCommented:
I just noticed you said copy, not move... Change mv to cp.

find /var/log/ -mtime -1 -name "loga*" -exec cp {} /home/username/ \;
0
 
TintinCommented:
rsync would be much more efficient for this purpose.

rsync -av loga* /home/username
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now