LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices



Reply
 
Search this Thread
Old 05-25-2011, 05:27 PM   #1
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,120

Rep: Reputation: 58
Backup One Liner find xargs cp


I will eventually script this but wanted to know where I screwed up when trying to get this oneliner to work. In a nutshell, I want to created a backup directory, find any files that have changed in the last 48hrs and them copy them to the newly made backup directory

PHP Code:
date=$(date +%m%d%Y)

mkdir -/home/test_dir/backup$date;find . -mtime 1|xargs -0 cp --/home/test_dir/backup$date 
When I do this I get:

PHP Code:
a bunch of junk !#$#$$#! 
File name too long 
I also tried:
PHP Code:
date=$(date +%m%d%Y)

mkdir -/home/test_dir/backup$date;find . -mtime 1|xargs -0 cp -{} /home/test_dir/backup$date 
When I do this I get:

help

Last edited by metallica1973; 05-25-2011 at 05:36 PM.
 
Old 05-25-2011, 06:25 PM   #2
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,120

Original Poster
Rep: Reputation: 58
I made some progress but am still not satisfied:

Code:
mkdir backup$date;find . -mtime 1|xargs -i cp -r  {} /home/saint/backup$date
It doesnt seem to be backing up every little detail like the ".bash_history" and files alike

Code:
.adobe
drwxr-xr-x 44 saint saint 12288 2011-05-25 18:16 backup05252011
-rw-------  1 saint saint  5233 2011-05-25 15:30 .bash_history
-rw-r--r--  1 saint saint 38363 2011-05-24 16:42 bash_history
-rw-r--r--  1 saint saint   220 2011-05-23 15:38 .bash_logout
-rw-r--r--  1 saint saint  3353 2011-05-23 15:38 .bashrc
drwx------  2 saint saint  4096 2011-05-24 16:31 .bogofilter
drwx------ 15 saint saint  4096 2011-05-25 15:38 .cache
drwx------  2 saint saint  4096 2011-05-05 17:44 .camel_certs
drwx------  3 saint saint  4096 2011-05-24 14:41 .compiz
drwxr-xr-x 17 saint saint  4096 2011-05-25 15:38 .config
-rw-r--r--  1 saint saint 23677 2011-05-25 18:13 davmail.log
-rw-r--r--  1 saint saint  1320 2011-05-24 16:56 .davmail.properties
drwx------  3 saint saint  4096 2011-05-23 16:47 .dbus
drwxr-xr-x  2 saint saint  4096 2011-05-23 16:47 Desktop
-rw-r--r--  1 saint saint    95 2011-05-25 10:37 .dmrc
drwxr-xr-x  8 saint saint  4096 2011-05-24 15:39 Documents
drwxr-xr-x  8 saint saint  4096 2011-05-25 17:07 Downloads
-rw-------  1 saint saint    16 2011-05-23 16:47 .esd_auth
??

Last edited by metallica1973; 05-29-2011 at 10:07 AM.
 
Old 05-26-2011, 07:46 AM   #3
choogendyk
Senior Member
 
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,189

Rep: Reputation: 105Reputation: 105
try something like

Code:
find . -mtime -1 -print0 | xargs -0 -I {} cp -pr {} /home/saint/backup$date
Note the "-1" for mtime.

One difficulty is that the find will find things down in directories that will then get copied into the backup directory without their enclosing directory. That's a bit confusing. But, suppose you have /dir and filex. Then inside /dir you have filex (a different instance). Suppose both filexes change. The find command will find both of them and copy them to the root of the backup directory. One of them will overwrite the other. You won't know which is which.

You can get around this difficulty by using cpio. I have a backup script that does something like what you are asking for, but it uses cpio. One line of the script is:

Code:
find . -mtime -1 | cpio -oa 2>/dev/null | ( cd ${backupdir} && cpio -imd )
That's on Solaris, but cpio should be similar on Linux. Try it. Check the man page to see what the details are and if there are differences.

Also double check the man page on find. You said 48 hours, but you were using 1, which is 24 hours. The "-1" is changed within the last 24 hours. You were using "1" without the "-".
 
Old 05-26-2011, 08:42 AM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
Use [code] tags, not [php] tags. The color is very annoying.
 
Old 05-29-2011, 10:13 AM   #5
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,120

Original Poster
Rep: Reputation: 58
Smile

sweet!

I will start using the code tags but blue isnt that bad I can always change to lavender or pink if you prefer.
 
Old 05-29-2011, 10:42 AM   #6
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Hanover, Germany
Distribution: Main: Gentoo Others: What fits the task
Posts: 15,650
Blog Entries: 2

Rep: Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095
I don't understand why you use xargs at all. Wouldn't something like
Code:
find . -mtime -1 -exec cp -r {} /home/saint/backup$date \;
work?
 
Old 05-30-2011, 09:35 PM   #7
choogendyk
Senior Member
 
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,189

Rep: Reputation: 105Reputation: 105
xargs is a faster and more efficient way of handling things. The "-exec" in the find will execute the command that follows it once for every item that is found. xargs takes the output of the find and handles it as efficiently as possible. Suppose that "find" finds 1,000 items, and the command in the "-exec" can handle 100 arguments (just as an example, not saying those are the right numbers for a particular command), then xargs will group the 1,000 items into groups of 100 and execute the command 10 times.

I have run into examples where the "-exec" failed and xargs handled it nicely. I've also run into examples where "-exec" was slow, and xargs was fast. That said, I do use both fairly often. It all depends on what I expect to run into. In the example in my previous post where I piped it to cpio, that is also an efficient way of handling the particular example.
 
1 members found this post helpful.
Old 05-30-2011, 09:50 PM   #8
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Hanover, Germany
Distribution: Main: Gentoo Others: What fits the task
Posts: 15,650
Blog Entries: 2

Rep: Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095
Thanks for the explanation.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
find | xargs | shred seems really slow Bertical Linux - Software 3 11-19-2010 10:36 AM
bash, find, xargs and built in commad 'cd' ::: Linux - Newbie 6 08-21-2009 08:04 AM
File name expansion with {} in gnu find and xargs anamericanjoe Linux - Software 2 09-16-2006 04:31 PM
clarification on find + xargs rm command dtra Linux - General 2 05-17-2006 06:56 AM
error with find . | xargs cbonar Linux - Newbie 7 12-09-2004 12:22 PM


All times are GMT -5. The time now is 05:27 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration