LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   script to compress a directory and then delete older than 7 days (https://www.linuxquestions.org/questions/linux-general-1/script-to-compress-a-directory-and-then-delete-older-than-7-days-786648/)

replica88 02-03-2010 06:41 AM

script to compress a directory and then delete older than 7 days
 
I have been trying to write a script that will take a directory, for example /accounts compress it into a .tar file with the filename containing the date of compression, for example accounts030210.tar and then place that file into a directory called /archive

I also want the script to delete files in /archive that are older than 7 days.

I have give it ago but suspect I went around it completely the wrong way, I would appreciate any help you could give me. Thanks

choogendyk 02-03-2010 07:19 AM

That's a reasonably straightforward script. Rather than asking us to give it to you, why not post what you did and ask for advice and corrections? That's a better learning process. Start by doing the individual commands by hand to see that they work as expected. Then put them in a script and test the script. Use the man pages. Start out with `man tar`, `man date`, and `man find`. Those will give you everything you need.

replica88 02-03-2010 08:26 AM

Yes, I need to get in the habit of reading those man pages

Code:


#!/bin/bash

#compress all files from /accounts to /archive named backup_"day-month-year"
tar -cvvf /archive/backup_`date +%d-%m-%Y`.tar.gz /accounts/

#delete all files from /archive older than 7 days
find /archive -ctime +7 -delete

This seems to work, is there a better way?

chrism01 02-03-2010 07:21 PM

If you want a gzipped file, you need to use the correct flag (-z) and I don't know why you'd use -v (verbose) twice or indeed even once in a program.
Also, why not use this date format for natural sorting: YYYYMMDD
Code:

tar -czf /archive/backup_`date +%Y%m%d`.tar.gz /accounts/
Incidentally, .tgz is an acceptable alternative to .tar.gz as a file extension.


All times are GMT -5. The time now is 12:49 PM.