LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-11-2013, 01:22 PM   #1
akamel
Member
 
Registered: Dec 2012
Posts: 100

Rep: Reputation: 1
Linux script - help!!


I need to create a bash script to run through crontab, to do the below job:

1- find files that are over 12 months old in /var
2- Tar them in /backup like: file_201201.tar.bz2
3- remove them after that
4- Send an email with the file deleted

Your help is highly appreciated!
Thanks in Advance
 
Old 08-11-2013, 01:36 PM   #2
jdkaye
LQ Guru
 
Registered: Dec 2008
Location: Westgate-on-Sea, Kent, UK
Distribution: Debian Testing Amd64
Posts: 5,465

Rep: Reputation: Disabled
Can you show us what steps you have taken. What have you done so far and where have you encountered problems?
jdk
 
Old 08-11-2013, 01:50 PM   #3
akamel
Member
 
Registered: Dec 2012
Posts: 100

Original Poster
Rep: Reputation: 1
It's a simple script I know. But I need to see the suggestions and output of you guys to gain more experience
That will help me to do it in the best way.

Thank you!
 
Old 08-11-2013, 02:31 PM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Quote:
Originally Posted by akamel View Post
It's a simple script I know. But I need to see the suggestions and output of you guys to gain more experience
That will help me to do it in the best way.

Thank you!
It usually works the other way round here. We see your output and make comments/suggest improvements, etc. Being given ready solutions is hardly ever a good way to learn.

Edit: BTW, if you do some google search, you'll see that someone had a very similar problem which was solved here on LQ (not including the mailing part)

Last edited by sycamorex; 08-11-2013 at 02:49 PM.
 
Old 08-11-2013, 02:59 PM   #5
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 akamel View Post
It's a simple script I know. But I need to see the suggestions and output of you guys to gain more experience
That will help me to do it in the best way.
No, YOU writing your own scripts will be the 'best way'. Much like a couple of your other threads, where you said you posted questions, since you didn't want to look something up on your own. We are not here to look things up for you, or write scripts for you.

And this is much like the thread you posted in February:
http://www.linuxquestions.org/questi...ct-4175449576/

...where you only posted what you wanted, but didn't show any effort. Some hints: read the man pages on find, tar, and mail. They can tell you the options on finding files older than certain dates/times, compressing them and removing the sources, and sending emails. And there are THOUSANDS of very easily-found scripting tutorials...one of which is even in my posting signature.
 
Old 08-11-2013, 06:00 PM   #6
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Quote:
Originally Posted by akamel View Post
I need to create a bash script to run through crontab, to do the below job:

1- find files that are over 12 months old in /var
2- Tar them in /backup like: file_201201.tar.bz2
3- remove them after that
4- Send an email with the file deleted

Your help is highly appreciated!
Thanks in Advance
hint: start with something like the following:

Code:
#!/bin/bash
some command here
an other command here
next command here
e-mail command here
exit
here is a remote connection script written just a few days ago for you to see howto write a script:

Code:
#!/bin/bash
#

###########################################################
### Created by Ray Brunkow Aug 4, 2013
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 or version 3 of the
# license, at your option.a
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
###
###########################################################

###########################################################
###
#
# The sole purpose of this script is to provide me remote access to 
# my wifes computer for management reasons anytime and place
# she has internet access.  Specifically while she is at work.  The 
# intent is NOT to break any law(s) or company rules.  Keeping her
# Linux computer connected with her MS Windows work environment
# is the primary reason for the creation of this script.
#
###
###########################################################

###########################################################
### setting variables
#
USER=`whoami`
RHOST=YOUR_URL_HERE	# set your own URL/IP address for your remote server
RUSER=user		# set your own remote user
#
###
###########################################################

###########################################################
### test for specific users and set appropriate port number
### This code from Firerat on Linuxquestions.org
#
case $(whoami) in
	user1)		PORT=11111;;
	user2)	        PORT=22222;;
	user3)		PORT=33333;;
	user4)		PORT=44444;;
	*)		echo "Error, user \"$(whoami)\" undefined";exit;;
esac
#
###
###########################################################

###########################################################
### side note.  If you run this with the -fN and you lose connection from computer_A
### you will lock up that specific port.  If that is a potential issue have the person
### on computer_A run it without the -fN.  You will have to edit the script before
### it is run.
#
#ssh -fNR "$PORT":localhost:22 "$RUSER"@"$RHOST" 
ssh -R "$PORT":localhost:22 "$RUSER"@"$RHOST"
exit
#
###
###########################################################
now please post what you have done so far, then we can provide further assistance.
 
Old 08-12-2013, 01:16 PM   #7
akamel
Member
 
Registered: Dec 2012
Posts: 100

Original Poster
Rep: Reputation: 1
Thanks for the advise!

#!/bin/bash

# This script will remove 12+ months old files after being compressed

# Firstly Introduce our variables
export TO=akamel@xyz.com
export SOURCE=/var/lib/mysql/
export DEST=/backup/local/
export SUBJECT= Old files backup process on the Server

# Here we find and compress those old files to the desired destination

I need to find and compress the files and move them in the dest directory -- Any suggestions plz ? :-)

# Her we remove the old files
rm -f files

# Send an email that everything is ok

printf "Dear all,\n\nThis is to let you know that all 12 months old files have been compressed and removed from server." | mail -s $SUBJECT $TO
 
Old 08-12-2013, 01:37 PM   #8
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 akamel View Post
Thanks for the advise!
Code:
#!/bin/bash
# This script will remove 12+ months old files after being compressed
# Firstly Introduce our variables
export TO=akamel@xyz.com
export SOURCE=/var/lib/mysql/
export DEST=/backup/local/
export SUBJECT= Old files backup process on the Server

# Here we find and compress those old files to the desired destination
I need to find and compress the files and move them in the dest directory -- Any suggestions plz ? :-)
Yes...we'll again suggest you read the man pages on the find and tar commands, and that you spell out your words. The man page on the find command will tell you how to find files that match date/time criteria, and all you really need to do is move the files, so the man page on the mv command will tell you that. Show some effort.
Quote:
Code:
# Her we remove the old files 
rm -f files
...and if you read the man page on the tar command (as was suggested before), you'd see how to not only compress the files, but remove the source after compression
Quote:
Code:
# Send an email that everything is ok
printf "Dear all,\n\nThis is to let you know that all 12 months old files have been compressed and removed from server." | mail -s $SUBJECT $TO
Again, read the man page on the mail command. The printf is not needed.
 
1 members found this post helpful.
Old 08-12-2013, 01:45 PM   #9
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Quote:
Originally Posted by akamel View Post
Thanks for the advise!

#!/bin/bash

# This script will remove 12+ months old files after being compressed

# Firstly Introduce our variables
export TO=akamel@xyz.com
export SOURCE=/var/lib/mysql/
export DEST=/backup/local/
export SUBJECT= Old files backup process on the Server

# Here we find and compress those old files to the desired destination

I need to find and compress the files and move them in the dest directory -- Any suggestions plz ? :-)

# Her we remove the old files
rm -f files

# Send an email that everything is ok

printf "Dear all,\n\nThis is to let you know that all 12 months old files have been compressed and removed from server." | mail -s $SUBJECT $TO
please use the code tags, makes reading much simpler. see my example above. [ code ] foo [ / code ] just remove the spaces and you get: [code foo[/code] like so.

you need to put a bit more effort into your script. have you followed the directions already provided by reading the man page for tar? i can add to that by saying read the man page for find as well. this is a very powerful tool

one more major thing to note you are mixing your code. you have BASH scripting at the top (#!/bin/bash) and you have C programing commands at the bottom
Code:
printf"foo"
and that is not complete. it should read:
Code:
printf("foo");
to be proper C programming language.

http://www.simplehelp.net/2008/12/15...iles-in-linux/

http://www.thegeekstuff.com/2009/03/...mand-examples/

http://content.hccfl.edu/pollock/unix/findcmd.htm

those three links will help with tar and find, the following link you should read first as it gives you the basics on howto code BASH scripts:

http://www.tldp.org/LDP/Bash-Beginne...ers-Guide.html

post back when you have more. you are going down the right path.

in BASH your variable settings are off:

Quote:
# Firstly Introduce our variables
export TO=akamel@xyz.com
export SOURCE=/var/lib/mysql/
export DEST=/backup/local/
export SUBJECT= Old files backup process on the Server
again note the quote tags, makes life much simpler to read:

Code:
# Firstly Introduce our variables
TO=akamel@xyz.com
SOURCE=/var/lib/mysql/
DEST=/backup/local/
SUBJECT="Old files backup process on the Server"
not the code tags this time: there is no need to export anything like that in BASH scripting.
 
Old 08-12-2013, 02:00 PM   #10
akamel
Member
 
Registered: Dec 2012
Posts: 100

Original Poster
Rep: Reputation: 1
Thanks Guys!

I read the man pages for Tar and Find, I still can't do my script well

I need to find and compress the files and move them in the dest directory, I'm trying to use the below code, but something is still missing!

find $SOURCE -type f -mtime +31 | xargs tar -jcvf backup.tar.bz2

Note: mtime +31 means 1 month, how can I make it 12 months?

The code need to be fixed I know, that's why I'm asking for your help
 
Old 08-12-2013, 02:03 PM   #11
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Quote:
Originally Posted by akamel View Post
Thanks Guys!

I read the man pages for Tar and Find, I still can't do my script well

I need to find and compress the files and move them in the dest directory, I'm trying to use the below code, but something is still missing!

find $SOURCE -type f -mtime +31 | xargs tar -jcvf backup.tar.bz2

Note: mtime +31 means 1 month, how can I make it 12 months?

The code need to be fixed I know, that's why I'm asking for your help
well lets do some logic here.... if you read the find command then you will understand that mtime + gives you the number of days. how many days are in a year. also for the last time before i abandon this thread start using the code flags.
 
1 members found this post helpful.
Old 08-12-2013, 02:08 PM   #12
akamel
Member
 
Registered: Dec 2012
Posts: 100

Original Poster
Rep: Reputation: 1
I don't know how to use the code flags, as I'm new here
I was asking if there is a better way than using the 365 days!

Did you checked the code?
 
Old 08-12-2013, 02:12 PM   #13
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 akamel View Post
Thanks Guys!
I read the man pages for Tar and Find, I still can't do my script well

I need to find and compress the files and move them in the dest directory, I'm trying to use the below code, but something is still missing!
Code:
find $SOURCE -type f -mtime +31 | xargs tar -jcvf backup.tar.bz2
Note: mtime +31 means 1 month, how can I make it 12 months?
The code need to be fixed I know, that's why I'm asking for your help
And we're asking you to read the man pages and show some effort. As lleb pointed out, doing +365 would give you one year. Putting "how to find files older than one year" into Google will ALSO tell you alot, if you tried to look.
Quote:
Originally Posted by akamel
I don't know how to use the code flags, as I'm new here
You select it, and click the "#" in the posting window, just like you would for bold, italic, or any other style. Since you've used other fonts and formatting in your other posts, it works the same way.
Quote:
I was asking if there is a better way than using the 365 days! Did you checked the code?
There are THOUSANDS of ways to do it...the find command is one that's pretty good. And you still haven't read the man page on tar, since you're not putting the option in to remove the source files upon compression.

Last edited by TB0ne; 08-12-2013 at 02:16 PM.
 
Old 08-12-2013, 02:18 PM   #14
akamel
Member
 
Registered: Dec 2012
Posts: 100

Original Poster
Rep: Reputation: 1
# Thank you very much guys! and I'm sorry for being new
Quote:
Originally Posted by TB0ne View Post
And we're asking you to read the man pages and show some effort. As lleb pointed out, doing +365 would give you one year. Putting "how to find files older than one year" into Google will ALSO tell you alot, if you tried to look.

You select it, and click the "#" in the posting window, just like you would for bold, italic, or any other style.

There are # I just need one way from the THOUSANDS of ways to do it...the find command is one that's pretty good. And you still haven't read the man page on tar, since you're not putting the option in to remove the source files upon compression.
 
Old 08-12-2013, 02:19 PM   #15
akamel
Member
 
Registered: Dec 2012
Posts: 100

Original Poster
Rep: Reputation: 1
I just need one way from THOUSANDS
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to execute a ssh script on Linux server from Windows through a bat script? wanna13e Programming 13 10-23-2009 02:41 AM
linux 9 and java script error - premature end of script header sibil Linux - Newbie 0 01-06-2004 04:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:13 PM.

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