LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-23-2009, 12:15 AM   #1
Mogget
Member
 
Registered: Dec 2008
Location: Norway
Distribution: Debian
Posts: 43

Rep: Reputation: 15
Bash script - how to use output from diff and find in context with cpio


Hey.

What i want to do is use the output from find, diff and then pipe it to cpio. Somewhat like this

Code:
find . -print > homefolderMonday
find . -print > homefolderTuesday
diff homefolderMonday homefolderTuesday | cpio --create --format=newc > /mnt/backup/backup.cpio
Problem is that the output from diff can't be piped directly since the output contains other things in addition to the folder and file changes.

example:

2684a2685
> ./.kde/share/apps/kopete/msnpictures/someuserblabla-com.png

So how do i remove the "2684a2685" and ">" part from diff output? I'm also open for other ways to do this so if you have sugestions tell me.

I've made a basic backup utility that backups every users /home folders to a specified mounted partition. It works somewhat like this. PS, this is not the complete code. I can provide it if necesary.

Code:
#!/bin/bash

# What user should be backed up?
USER=Mogget
# Users home directory
USERHOME=/home/$USER
# Where to backup to
BACKUPDIR=/mnt/backup
# What date is it today?
DATE=`date -u +%m%d%y`
# Create users directory in backup directory?
CRTUSERDIR=0
# Create todays directory in users backup directory?
CRTUSERTODAY=0
# If 0 the backup process will be aborted after directory checking
DOBACKUP=0

if [ -d $BACKUPDIR/$USER ]; then
     echo "$BACKUPDIR/$USER exist."
else
     echo "Creating $BACKUPDIR/$USER."
     mkdir $BACKUPDIR/$USER
fi

if [ -d $BACKUPDIR/$USER/$DATE.bak
     echo "Backup already been done, exiting backup script."
     DOBACKUP=1
else
     echo "Creating $BACKUPDIR/$USER/$DATE.bak."
     mkdir $BACKUPDIR/$USER/$DATE.bak
fi

if [ $DOBACKUP = "0" ]; then
     cd $USERHOME
     echo "Making a list of $USERHOME."
     find . -print > $BACKUPDIR/$USER/$DATE.bak/$DATE.list
     echo "Using $BACKUPDIR/$USER/$DATE.bak/$DATE.list to backup with cpio."
     cat $BACKUPDIR/$USER/$DATE.bak/$DATE.list | cpio --create --format=newc > $BACKUPDIR/$USER/$DATE.bak/$DATE-backup.cpio
     echo "Updating last update date in $BACKUPDIR/$USER/.last-backup."
     date -u +%m%d%y > $BACKUPDIR/$USER/.last-backup
     echo "Done backing up for today."
fi
The point of $BACKUPDIR/$USER/.last-backup is so that the script can retrieve the last backup list made so we can diff todays list and last backups list and run it through cpio

Any help and pointers will be gladly taken sorounding different ways to do this.

Thanks for the help in advance.

Last edited by Mogget; 01-23-2009 at 12:18 AM.
 
Old 01-23-2009, 04:48 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949
You can pipe the output through egrep, using a regex to match only the filenames. Something like:

Code:
diff homefolderMonday homefolderTuesday | egrep -o "\./.+$" | cpio --create --format=newc > /mnt/backup/backup.cpio
This assumes the file names all start with ./ and aren't followed by anything else on the line.

Edit: It seems that "diff -n" will print the lines without the leading > character, so you could make the grep expression even simpler; something like egrep -o "^\." (matches any line starting with a period) would probably be all you need, to filter out non-file lines.

Last edited by David the H.; 01-23-2009 at 04:58 AM.
 
Old 01-23-2009, 07:19 AM   #3
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,766

Rep: Reputation: 475Reputation: 475Reputation: 475Reputation: 475Reputation: 475
I think diff is simply the wrong tool for the job. Either comm or cmp would probably be more suited, but there are surely still better ways to compare the contents of two directories, such as dircomp:
http://sourceforge.net/projects/dircomp
 
Old 01-23-2009, 08:16 AM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
diff, the way you are trying to use it, is going to show you files that have been removed, and these are going to be difficult to backup. find, on the other hand, should be able to directly report those files which are new or modified in the most recent minutes or days:
Code:
find . -ctime -1 -print
prints the full filespec of all files created or modified in the last day.

--- rod.
 
Old 01-23-2009, 10:38 AM   #5
Mogget
Member
 
Registered: Dec 2008
Location: Norway
Distribution: Debian
Posts: 43

Original Poster
Rep: Reputation: 15
Thank you for the suggestions.
I knew that diff would output folders that has been removed to so that's a blunder from my side.

I'll look closer into the find -ctime function since that would make my script easier to work with. Thank you for your time.
 
  


Reply

Tags
backup, cpio, diff, find


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
Bash Script Missing Output Slick666 Programming 1 10-05-2007 07:09 AM
Test output from bash script estratos Programming 6 11-16-2006 09:01 PM
Bash Script, calculate output. eldaria Programming 13 07-20-2006 09:26 PM
grab FTP output in bash script bokini Linux - General 2 02-03-2006 02:11 PM
Quick question: full-context diff? overbored Linux - Software 0 09-27-2004 04:11 AM


All times are GMT -5. The time now is 07:28 AM.

Main Menu
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