Copy Files As Perfect Backups in Another Directory


cp -a

You might be thinking right now that cp would be useful for backing up files, and that is certainly true (although better programs exist, and we'll take a look at one of themrsyncin Chapter 15, "Working on the Network"). With a few lines in a bash shell script, however, cp can be an effective way to back up various files and directories. The most useful option in this case would be the -a option (or --archive), which is also equivalent to combining several options: -dpR (or --no-dereference --preserve --recursive). Another way of thinking about it is that -a ensures that cp doesn't follow symbolic links (which could grossly balloon your copy), preserves key file attributes such as owner and timestamp, and recursively follows subdirectories.

$ pwd /home/scott $ ls -l drwxr-xr-x     216 2005-10-21 11:31 libby drwxr-xr-x     216 2005-09-29 23:17 music $ ls -lR libby libby: total 312 -rw-r--r--   73786 2005-10-20 12:12 arrowrock.jpg -rw-r--r--   18034 2005-04-19 00:57 libby.jpg -rw-r--r--  198557 2005-04-19 00:57 on_couch.jpg drwxr-xr-x     168 2005-10-21 11:31 on_floor libby/on_floor: total 764 -rw-r--r--  218849 2005-10-20 16:11 on_floor_01.jpg -rw-r--r--  200024 2005-10-20 16:11 on_floor_02.jpg -rw-r--r--  358986 2005-10-20 16:11 on_floor_03.jpg $ cp -a libby libby_bak $ ls -l drwxr-xr-x    216 2005-10-21 11:31 libby drwxr-xr-x    216 2005-10-21 11:31 libby_bak/ drwxr-xr-x    216 2005-09-29 23:17 music $ ls -lR libby_bak libby: total 312 -rw-r--r--   73786 2005-10-20 12:12 arrowrock.jpg -rw-r--r--   18034 2005-04-19 00:57 libby.jpg -rw-r--r--  198557 2005-04-19 00:57 on_couch.jpg drwxr-xr-x     168 2005-10-21 11:31 on_floor libby/on_floor: total 764 -rw-r--r-- 218849 2005-10-20 16:11 on_floor_01.jpg -rw-r--r-- 200024 2005-10-20 16:11 on_floor_02.jpg -rw-r--r-- 358986 2005-10-20 16:11 on_floor_03.jpg 


Note

Yes, you've probably figured it out already, but let me confirm: Libby is my dog, a cute lil' shih-tzu who eventually ends up in some way in almost everything I write. To see her, check out http://www.granneman.com/go/libby.




Linux Phrasebook
Linux Phrasebook
ISBN: 0672328380
EAN: 2147483647
Year: 2007
Pages: 288

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net