I have to backup and delete old log files every month. I will delete files older than 6 months and backup files older than 2 months in as a zip file.
I am trying to write a script that will automate and do it every month instead of me doing it manually every time.
I have UNIX commands on how to do it, but I need to put it into script file which will run automatically on the day specified.
You can schedule a cronJob for daily , which runs command inside script such as
find foldername -mtime +120 -name "*.log" -exec gzip {} \;
Above will take care of archiving all files older than 120 days. Part inside quotes after name can be modified as per your requirement and so does the +120.
find foldername -mtime +180 -name "*" -exec rm {} \;
Above will remove all file inside foldername older than 180 days.
For automation part , you can look at wiki link provided in the answer below. Though i will include it in my answer too.
You can use crontab to schedule commands (https://en.wikipedia.org/wiki/Cron)
You can use crontab to schedule commands (https://en.wikipedia.org/wiki/Cron)
You can add entry typing crontab -e and use it to schedule jobs, after adding your unix commands to a script.
For example, if you have a /home/test/test.sh file, you can run it everyday by adding the below to your crontab :
0 0 * * * /home/test/test.sh
Related
The script in file modBackup.sh does not run completely when started by cron, the result is a corrupted tar.gz file that is half the size of this one if I run manually. In any case, its size is many times smaller than the one started manually, but still creates some content that can not be opened normally, archive is damaged
file modBackup.sh:
#!/bin/sh
find /home/share/ -mmin -720 -type f -exec tar -rvf /mnt/archives/`date +%F`-modified.tar.gz "{}" +
Тhe behavior of the automatic one seems to be interrupted and does not end.
When I run it manualy, the script creates a genuine archive as [current date]-modified.tar.gz
Here is the crontab -e:
00 18 * * 1-5 /home/myScripts/modBackup.sh
Edit:
There is no information in the logs except that crond has started
neither in the mail log, nor in the cron, nor in the messages
(I use very old CentOS :( but I don't think this is the reason for the error).
For testing only: I added %H%M of the file name in the script and did the following:
I ran it manually: sh /home/myScripts/modBackup.sh
and set with crontab -e to run a two minutes later the same command
After a few minutes, two files appeared that grew at the same time, but then the one created by cronjob
stopped growing
(two files).
I use the same GUI tool (Archive Manager) to open in both cases.
Тhe file, created by manually starting the script, opens (manually started), but the other one, from cronjob cannot, even after I changed the extension, the error is 'unexpected EOF in archive' (auto started)
Suggesting to include the users's environment context with $PATH and other critical environment variables for the application to work.:
modBackup.sh:
#!/bin/sh
source ~/.profile
find /home/share/ -mmin -720 -type f -exec tar -rvf /mnt/archives/`date +%F`-modified.tar.gz "{}" +
I found that in the cron environment the "find" command misinterprets filenames containing specific characters, even with the explicit change of the encoding with add at the beginning of the script line "export LANG = en_US.UTF-8; LC_CTYPE=...". With many other combinations and attempts I had no success.
That's why I left the "find" command and use the tar command with an option to archive modified files. This way works perfect now:
fromDate = $(date --date = '15 hours ago')
/bin/tar -N "$fromDate" -zcf /mnt/archives/`date +% F-% H% M`-share.modified.tar.gz /home/share/
I have a cron job on my computer set to run on an external volume each night. I would like to add an exclude list as part of this. However, I can't figure out where to place the list in order to have it read by the rsync command. I've tried placing it in my /usr/local/bin/ which is where the script that my cron job uses is, and putting just the name of the exclude file in the script. I've also tried putting the script on the external volume and providing the entire path. I know the list itself works because I tested it with files on my desktop. I would appreciate any help! The rsync script itself works as expected, I just need to figure out this one issue.
This is the code:
DATE=$(date +%Y%m%d-%H%M%S)
ICE="/Volumes/ice"
RTG="/Volumes/ice/rtg/"
LOGFILE="/Users/diunt-02/Desktop/${DATE}_ICEnightmoves.log"
find "${ICE}" -type d -iname 'X*X' -exec rsync -rthvP --exclude-from= 'exclude.txt' --exclude=".*" --exclude="jpegs" --exclude="*.jpg" --log-file="${LOGFILE}" --log-file-format="${DATE} '%f' %l " --remove-source-files "{}/" "${RTG}" \;
I am having issue storing my server backup on a storage VPS. My server is not deleting old backup folders and the storage is getting full and the backup fails in mid way. My runs once every week.
Can anyone help me create a cron job script on that deletes folder older than 7 days and runs one day before backup and delete old folders.
Any help appreciated.
For example, the description of crontab for deleting files older than 7 days under the /path/to/backup/ every day at 4:02 AM is as follows.
02 4 * * * find /path/to/backup/* -mtime +7 -exec rm {} \;
Please make sure before executing rm whether targets are intended files. You can check the targets by specifying -ls as the argument of find.
find /path/to/backup/* -mtime +7 -ls
mtime means the last modification timestamp and the results of find may not be the expected file depending on the backup method.
I need to create a bash script to run another script command if none of the files in the directory have been created within 30 mins.
I am not sure of the code I need but it needs to find and execute if not matched. -
find /folder/to/watch/* if-not 30 mins -exec fixscript.sh or something?
When the script is ran I want it to check the files in the folder , if a file has not been created within the last 30 mins then to run the fixscript.sh
Thanks in advance
I don't think this can be combined to a single find statement. The following would work, with the caveat that if a file is modified, it would be detected as "newer than 30 minutes".
if [ `find /folder/to/watch -mmin -30 | wc -l` -eq 0 ]; then
/path/to/fixscript.sh
fi
Linux/Unix does not have an independent file creation attribute. Some filesystems might have it, though, but it can't be accessed from shell without c code and call to stat(). This uses "file modified" timestamp, which gets changed on not only file creation but also file edit.
Hannu
In Linux you don't have metadata for creation time, but only for last modification time, last file status change and last access time.
You can do:
if test ! [`find "your_file" -mmin +30`]
then
`path/to/your/script.sh`
fi
the -mmin option is giving you last modification time.
I have a script that makes files every day like:
last-2014-08-01.csv
out-2014-08-01.csv
Following this idea:
http://www.idevelopment.info/data/Oracle/DBA_tips/Unix/UNIX_7.shtml
30 12 * * * /u01/app/oracle/bin/rman_backup.pl > /u01/app/oracle/log/rman_backup_$(date +\%Y\%m\%d).log 2>&1
I came with this idea for a cron:
#daily find /app/calculo/api/last-$(date +\%Y-)*.csv -mtime +30 -delete
#daily find /app/calculo/api/out-$(date +\%Y-)*.csv -mtime +30 -delete
What is missing is that is not deleting any file when I test it.
thanks
Can you double check if you quoted the percent sign correctly?
https://unix.stackexchange.com/questions/29578/how-can-i-execute-date-inside-of-a-cron-tab-job
In general I'd suggest you to write a shell script which contains all the logic and just to call it from cron. You will be free from quoting problems and debugging will also be easier.
#daily /path/to/cleanup-csv.bash
Be sure to remember to set the $PATH environment variable correctly in your shell script. The crontab sets minimal number of directories to $PATH.