LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   backup script help (http://www.linuxquestions.org/questions/linux-general-1/backup-script-help-4175450951/)

tsitras 02-20-2013 08:16 AM

backup script help
 
hi. in a folder i do have a whole lot of files created every day. i need to compress files older than 30 days in tgz and delete the compressed files.

#!/bin.bash

FILES=`find . -mtime -30 | xargs`
tar --no-recursion -czf backup_feb_2013.tgz "${FILES}"
rm -rf "${FILES}"

the compressed files have the following format

g303-20130205-060552-1360037152.419.wav

i am stuck on how to edit the script so it will automatically create the month of the backup. Now the naming is manual.

shivaa 02-20-2013 09:23 AM

Instead of doing this using a script or manually, you can go with logrotate (see manual here) utility.

Second thing, in your script, take a note that -mtime -30 will take files modifed within last 30 days not before 30 days. So use +30 day. To check:
Code:

~$ find . -mtime +30 -print
And then in your script:
Code:

#!/bin/bash
find . -mtime +30 -exec mv '{}' /path/to/dir \;
tar -cvf oldfiles.tar /path/to/dir; gzip oldfiles.tar
mv oldfiles.tar.gz oldfile.$(date +%Y%m%d%H%M).tar.gz


TB0ne 02-20-2013 09:38 AM

Quote:

Originally Posted by tsitras (Post 4895944)
hi. in a folder i do have a whole lot of files created every day. i need to compress files older than 30 days in tgz and delete the compressed files.
Code:

#!/bin.bash
 
FILES=`find . -mtime -30 | xargs`
tar --no-recursion -czf backup_feb_2013.tgz "${FILES}"
rm -rf "${FILES}"

the compressed files have the following format

g303-20130205-060552-1360037152.419.wav

i am stuck on how to edit the script so it will automatically create the month of the backup. Now the naming is manual.

You should read the man page on the date command, and the bash scripting tutorial on tldp:
http://tldp.org/LDP/abs/html/timedate.html

Pay particular attention to the format options on the date command, and in your case specifically, the %b, %B, and %m flags. Try this for example:
Code:

#!/bin/bash

monthlong=`date +%B`
monthshort=`date +%b`
monthnum=`date +%m`
year=`date +%Y`
echo "Month Long Form -  $monthlong"
echo "Month Abbrev. - $monthshort"
echo "Month Number - $monthnum"
echo "Year - $year"

echo "Backup filename = backup_$monthshort"_"$year.tgz"

# Read the 'date' man page for more formatting options.
exit 0

If you put the month/year into variables, that leaves you a good path in your script for tests later, should you want to. Things like checking what year it is and doing some action, or (for example) if it's a leap year, and the end of the month in February is a different date.

And you should also read the man page on the tar command, and look into the "--remove-files" flag, which will delete the files after they're added to the tar archive.


All times are GMT -5. The time now is 07:55 PM.