LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-20-2013, 08:16 AM   #1
tsitras
Member
 
Registered: Mar 2003
Distribution: Debian Squeeze
Posts: 182

Rep: Reputation: 30
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.
 
Old 02-20-2013, 09:23 AM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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
 
1 members found this post helpful.
Old 02-20-2013, 09:38 AM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,617

Rep: Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963
Quote:
Originally Posted by tsitras View Post
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.

Last edited by TB0ne; 02-20-2013 at 09:41 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What happens with backup-manager.org (very useful backup script)? Murz Linux - Software 3 07-27-2010 06:46 AM
Newbie trying to write a simple backup script to backup a single folder Nd for school stryker759a Linux - Newbie 2 09-16-2009 08:52 AM
Need help with script to organise files into folders as part of DVD backup script jasybee2000 Linux - Newbie 5 06-15-2009 07:29 PM
Backup script camry Linux - Software 2 06-04-2009 03:20 AM
how to create backup MYSQL Script to backup my database for every 1hour RMLinux Linux - Newbie 3 11-20-2008 10:13 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration