LinuxQuestions.org
Help answer threads with 0 replies.
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 04-09-2013, 06:27 AM   #1
jimwarlock
LQ Newbie
 
Registered: Apr 2013
Posts: 11

Rep: Reputation: Disabled
Unhappy Creating a Backup Script in Bash Terminal


Hey guys, long time reader first time poster.

I play around with Unix a bit, mostly for fun. I'm doing a few online tutorials to further my knowledge and have run into a problem i just cant get my head around. Am hoping you guys can help!

So the problem is as follows:

You have to create a script that when run will copy all files in your current working directory and put them in a backup folder.
Now the catch is those files are filtered by an input from the user which related to when the file was last modified.

Something like:

backup.sh -m 02 -y 2011
will backup all files that were modified after the feb, 2011

hope that makes sense. If you've any questions please ask.

I'm racking my brain and just cant work out how to go about this. Damn frustrating
 
Old 04-09-2013, 08:17 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
The key cmd there is 'find' http://linux.die.net/man/1/find, which has many options to select files by date eg 'mtime'
Here's some good bash links
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
Old 04-09-2013, 08:29 AM   #3
jimwarlock
LQ Newbie
 
Registered: Apr 2013
Posts: 11

Original Poster
Rep: Reputation: Disabled
Thanks heaps for your reply.

I figured id be relying on that command for this.

I had something like

Find * mtime -3 | cp /backup

I understood that as find all files that were modified in the last 3 days and copy those to the /backup directory
Now i was getting errors with that first of all

Second im stuck really on going that extra step to make mtime an input by the user.

Also if im able to use months and years in the mtime filter too

Sorry thinking out loud here. Some of these answers are possibly in those links and i will check them out thanks again
 
Old 04-09-2013, 08:36 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Just a few hints
Code:
find /path -mtime -3 ...
See the 'Tests' section of the find page for the use of +n, -n or just n.
You must specify the path for find to start at and it can't be wildcard like '*'
 
Old 04-09-2013, 08:44 AM   #5
jimwarlock
LQ Newbie
 
Registered: Apr 2013
Posts: 11

Original Poster
Rep: Reputation: Disabled
thanks for that once again.

i think the find command is what was throwing me. haven't used it a whole lot.
Adding a path seemed to help but im still stuck on parsing variables into it to change the files to find based on a given modify date.

sudo example, find /home mtime -(march 2012)...
 
Old 04-09-2013, 06:19 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Well, the usual arrangement for a backup system is to do a rolling backup of eg all files lees than x days old, hence the section I referred you to.
That way, no files get missed out.

If you want the user to be able to specify a particular date or date range, you're going to have to read up on the date cmd http://linux.die.net/man/1/date and read those links above to figure how to do date calcs, then convert to n days ago to be able to use find to grab them.
I guess it would be a good little programming project; its do-able but sufficiently non-trivial to be interesting and a good learning experience.
 
Old 04-10-2013, 12:06 AM   #7
jimwarlock
LQ Newbie
 
Registered: Apr 2013
Posts: 11

Original Poster
Rep: Reputation: Disabled
Well that's why im taking it on and trying to learn a bit more.
Also why im here trying to get help.

Thanks for your comments chris
 
Old 04-10-2013, 01:32 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
probably you can try rsync also...
 
  


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
[SOLVED] How to make a bash script keep running in same terminal after it calls second script? JohnRock Linux - Newbie 4 06-25-2010 09:16 AM
Bash - backup script - need help with my if-then-else :D fruitwerks Programming 12 04-11-2009 01:34 PM
Backup script in bash gauge73 Programming 13 10-17-2005 06:25 AM
Help creating a Bash script gothicreader Linux - Newbie 4 10-10-2003 01:36 PM
need help creating a bash script zibertron Linux - General 3 01-31-2003 09:23 AM

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

All times are GMT -5. The time now is 08:11 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