Results 1 to 10 of 10

Thread: Rsync Backup --exclude=/home/*/.*

  1. #1
    Join Date
    Mar 2009
    Beans
    927
    Distro
    Ubuntu 12.04 Precise Pangolin

    Lightbulb [SOLVED] Rsync Backup Script

    It should have been --exclude=/*/.* rather than --exclude=/home/*/.* The finished backup script can be found below, change WRITE='/media/backup' to wherever you want to back it up to (warning: if you back up to somewhere inside /home then it will recur one every time you backup, to avoid this add - /path/to/backup to the exclude-list). Thanks to unutbu and stroyan for correcting me.


    /usr/local/bin/backup-home
    Code:
    #!/bin/bash
    
    ##### CONSTANTS #####
    NAME="${0##*/}"
    DESCRIPTION=
    VERSION=0.1
    LOCATION="$(readlink -f $0)"
    LOCATION="${LOCATION%$NAME}"
    EXCLUDE="${LOCATION}exclude-list"
    WRITE='/media/backup'
    
    ##### MAIN CODE #####
    guest=$(echo $1 | sed 's_[^#]*@\([^#]*\)_\1_')
    
    if [ "$(whoami)" != 'root' ]; then
    	echo >&2 "Error: You must have root privileges to run this program."
    	exit 1
    fi
    while [ ! -d "$WRITE" ]; do
    	echo >&2 "Please mount backup device at $WRITE and press [ENTER] to continue.
    Press [ESC] to postpone backup."
    	if [ "$(wait-keypress )" == '' ]; then  # Escape/Enter
    		exit 0
    	fi
    done
    echo "The backup may take some time, please be patient..." 
    if [ "$1" == '' ]; then
    	rsync --delete -act /home/ $WRITE/$HOSTNAME:\ Home\ Backup/ --exclude-from="$EXCLUDE"
    else rsync --delete -act $1:/home/ $WRITE/$guest:\ Home\ Backup/ --exclude-from="$EXCLUDE"
    fi
    
    if [ "$?" == 0 ]; then
    	echo "Backup completed successfully!"
    	exit 0
    else 
    	echo "Backup did not complete successfully."
    	exit 1
    fi


    /usr/local/bin/exclude-list
    Code:
    + /*/.alaises
    + /*/.profile
    + /*/.*rc
    + /*/.devilspie/
    - /*/.*
    - /lost+found
    Last edited by Penguin Guy; July 27th, 2009 at 10:46 AM.

  2. #2
    Join Date
    Mar 2008
    Beans
    4,715
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Rsync Backup --exclude=/home/*/.*

    See if this command lists all the files you want (and none that don't want):
    Code:
    cd /home
    find . -regex "./[^/]+/[^.].*"
    If that command succeeds, then something like
    Code:
    find . -regex "./[^/]+/[^.].*" |  rsync -ac --files-from - /home/ $WRITE/$HOSTNAME:\ Home\ Backup/ --exclude=lost+found
    may do what you want.

    I see in your rsync command that you wish to backup files or directories called '.aliases'. For that perhaps use
    Code:
    rsync -ac  --include='.aliases' -f 'hide,! */' /home/ $WRITE/$HOSTNAME:\ Home\ Backup/
    PS: Hmm... A bit of testing seems to suggest using --files-from may obstruct your ability to use the --delete flag...
    Last edited by unutbu; July 7th, 2009 at 06:05 PM.

  3. #3
    Join Date
    Mar 2009
    Beans
    927
    Distro
    Ubuntu 12.04 Precise Pangolin

    Question Exact Filepaths From 'find' Command?

    Thanks, it seems to work quite well but is there any way to get exact file paths rather than relative ones from the find command?

  4. #4
    Join Date
    Mar 2008
    Beans
    4,715
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Rsync Backup --exclude=/home/*/.*

    Yes:
    Code:
    sudo find /home -regex "/home/[^/]+/[^.].*"

  5. #5
    Join Date
    Mar 2009
    Beans
    927
    Distro
    Ubuntu 12.04 Precise Pangolin

    Question Re: Rsync Backup --exclude=/home/*/.*

    Thanks! All now works great apart from, as you said, the --delete parameter not working. It may be possible to fix this by adding a / to the end of all directories but not to the end of files. This could be done with a another command but I was wondering if find had a built-in function to do this? I have searched but found nothing on the internet or in the man page.

    Thanks for all the help.

  6. #6
    Join Date
    Mar 2008
    Beans
    4,715
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Rsync Backup --exclude=/home/*/.*

    How about this:

    Code:
    # Copy everything in home that is not a hidden file or directory, or the lost+found directory:
    rsync -ac --delete --exclude=lost+found --exclude='.*' /home/ $WRITE/$HOSTNAME:\ Home\ Backup/ 
    
    # Copy the .aliases files or directories:
    rsync -ac  --include='.aliases' -f 'hide,! */' /home/ $WRITE/$HOSTNAME:\ Home\ Backup/
    
    # Copy "2nd-level" hidden files:
    find /home -regex "/home/[^/]+/[^.].*/\..*" | rsync -ac --files-from - /home/ $WRITE/$HOSTNAME:\ Home\ Backup/
    The first command includes the --delete flag.

  7. #7
    Join Date
    Oct 2007
    Location
    Fort Collins, CO, USA
    Beans
    480
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Rsync Backup --exclude=/home/*/.*

    rsync alone can select the files that you want.
    Use "/*/.*" to refer to files and directories that start with . and reside one directory level below the root of the transfer in the /home/ directory.
    Patterns that don't start with a "/" will be applied to file names in all directories. That means that your "--exclude=lost+found" will apply to all files and directories at any level. You may want to use "/" anchors for those other patterns as well.

    rsync --delete -act /home/ $WRITE/$HOSTNAME:\ Home\ Backup/ --exclude=lost+found --include=.aliases --exclude="/*/.*"

  8. #8
    Join Date
    Mar 2008
    Beans
    4,715
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Rsync Backup --exclude=/home/*/.*

    This is a much better solution. Thanks, stroyan!

  9. #9
    Join Date
    Mar 2009
    Beans
    927
    Distro
    Ubuntu 12.04 Precise Pangolin

    Talking [SOLVED] Rsync Backup --exclude=/*/.*

    Quote Originally Posted by stroyan View Post
    rsync alone can select the files that you want.
    Use "/*/.*" to refer to files and directories that start with . and reside one directory level below the root of the transfer in the /home/ directory.
    Patterns that don't start with a "/" will be applied to file names in all directories. That means that your "--exclude=lost+found" will apply to all files and directories at any level. You may want to use "/" anchors for those other patterns as well.

    rsync --delete -act /home/ $WRITE/$HOSTNAME:\ Home\ Backup/ --exclude=lost+found --include=.aliases --exclude="/*/.*"
    Thanks - that works perfectly! I see where I messed up!

  10. #10
    Join Date
    Mar 2009
    Beans
    927
    Distro
    Ubuntu 12.04 Precise Pangolin

    Question Re: Rsync Backup --exclude=/home/*/.*

    One small problem: When I backup using the --delete flag it takes ages, I'm guessing it's including files that haven't changed in the backup for some reason. Anyone know why?

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •