LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Creating a Backup Script in Bash Terminal (https://www.linuxquestions.org/questions/linux-newbie-8/creating-a-backup-script-in-bash-terminal-4175457478/)

jimwarlock 04-09-2013 06:27 AM

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

chrism01 04-09-2013 08:17 AM

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/

jimwarlock 04-09-2013 08:29 AM

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

chrism01 04-09-2013 08:36 AM

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 '*'

jimwarlock 04-09-2013 08:44 AM

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)...

chrism01 04-09-2013 06:19 PM

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.

jimwarlock 04-10-2013 12:06 AM

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

pan64 04-10-2013 01:32 AM

probably you can try rsync also...


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