Thursday, May 2, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 3741  / 2 Years ago, wed, july 6, 2022, 6:33:46

I want to make a cronjob that makes a copy of every file in /var/log, and puts it in /etc/bak. That i do by making an sh file saying



cp /var/log/*.* /etc/bak


I would like these files not to be overwritten, but to be unique. To do this i figured i want to put the date/time in the filename. Is there any way to make that possible on a batch file copy?


More From » cron

 Answers
7

How about putting every batch of log files in a separate directory. You could do it using:



dir=/etc/bak/`date "+%Y-%m-%d_%H.%M.%S"`
mkdir "$dir"
cp /var/log/*.* $dir


If you want to prefix all the copied files with a timestamp you can do:



(cd /var/log && ls -1 *.*) | awk -v date=`date "+%Y-%m-%d_%H.%M.%S"` '{ print "cp /var/log/" $0 " /etc/bak/" date  $0}' | sh

[#28034] Wednesday, July 6, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
deringach

Total Points: 412
Total Questions: 107
Total Answers: 101

Location: Palestine
Member since Tue, Jul 20, 2021
3 Years ago
;