07592161981m
asked on
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
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
ASKER
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 .
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
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
which selects all files *loga* from mylogs, newer than 2 days, but older than 1 day, and runs a cp command on them.
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/
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/
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
rsync would be much more efficient for this purpose.
rsync -av loga* /home/username
rsync -av loga* /home/username
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.