Linux - Server This forum is for the discussion of Linux Software used in a server related context. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
05-25-2011, 05:27 PM
|
#1
|
Senior Member
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190
Rep:
|
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 -p /home/test_dir/backup$date;find . -mtime 1|xargs -0 cp -r -t /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 -p /home/test_dir/backup$date;find . -mtime 1|xargs -0 cp -r {} /home/test_dir/backup$date
When I do this I get:
help
Last edited by metallica1973; 05-25-2011 at 05:36 PM.
|
|
|
05-25-2011, 06:25 PM
|
#2
|
Senior Member
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190
Original Poster
Rep:
|
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.
|
|
|
05-26-2011, 07:46 AM
|
#3
|
Senior Member
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197
Rep:
|
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 "-".
|
|
|
05-26-2011, 08:42 AM
|
#4
|
LQ 5k Club
Registered: Sep 2009
Posts: 6,443
|
Use [code] tags, not [php] tags. The color is very annoying.
|
|
|
05-29-2011, 10:13 AM
|
#5
|
Senior Member
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190
Original Poster
Rep:
|
sweet!
I will start using the code tags but blue isnt that bad I can always change to lavender or pink if you prefer.
|
|
|
05-29-2011, 10:42 AM
|
#6
|
Moderator
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
|
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?
|
|
|
05-30-2011, 09:35 PM
|
#7
|
Senior Member
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197
Rep:
|
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.
|
05-30-2011, 09:50 PM
|
#8
|
Moderator
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
|
Thanks for the explanation.
|
|
|
All times are GMT -5. The time now is 12:08 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|