rsync: Use filters to exclude top-level directory but include some of its subdirectories

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












2















I want to back up my /home directory with rsync. I have read rsync's man page and decided to use filter rules for this task.



What I would like to achieve: Exclude all files and directories in the Repos directory but keep all pull_all.sh files and output directories --- regardless where they are located within the Repos directory.



So far, I have ended up with following filter list, but this backs up only the pull_all.sh files but not the output directories:



# Files prefixed with "+ " are included. Files prefixed with "- " are excluded.
#
# The order of included and excluded files matters! For instance, if a folder
# is excluded first, no subdirectory can be included anymore. Therefore,
# mention included files first. Then, mention excluded files.
#
# See section "FILTER RULES" of rsync manual for more details.


# Included Files

# TODO: This rules do not work properly!
+ output/***
+ pull_all.sh
- Repos/**

# Excluded Files

- .android
- .cache
...


I use the filter list in my script run_rsync.sh:



#!/bin/bash

date="$(date +%Y-%m-%d)"
hostname="$(hostname)"

# debug_mode="" # to disable debug mode
debug_mode="--list-only"

# Note: With trailing "/" at source directory, source directory is not created at destination.
rsync $debug_mode --archive --delete --human-readable --filter="merge $hostname.rsync.filters" --log-file=logfiles/$date-$hostname-home.log --verbose /home backup/


Unfortunately, the existing StackExchange threads have not solved my problems:



  • https://stackoverflow.com/questions/8270519/rsync-exclude-a-directory-but-include-a-subdirectory

  • Using Rsync include and exclude options to include directory and subdirectory but exlude files in subdirectory

What's going wrong here?



[Update] Here is an example how the home directory looks like and which files to keep and which files to ignore:



user@hostname:~$ tree /home/ | head
/home/
└── user
├── Desktop -> keep this
│   ├── file1 -> keep this
│   └── file2 -> keep this
├── Documents -> keep this
├── Repos
│   ├── pull_all.sh -> keep this
├── subdir1
│ ├── output -> keep this
├── subdir2
├── another_subdir
├── output -> keep this
├── subdir3 -> do not keep (because does not contain any "output")
├── file3 -> do not keep









share|improve this question
























  • Also take a look at Bash scripting and rsync: how to include just some folders and subfolders...

    – roaima
    Feb 27 at 8:28
















2















I want to back up my /home directory with rsync. I have read rsync's man page and decided to use filter rules for this task.



What I would like to achieve: Exclude all files and directories in the Repos directory but keep all pull_all.sh files and output directories --- regardless where they are located within the Repos directory.



So far, I have ended up with following filter list, but this backs up only the pull_all.sh files but not the output directories:



# Files prefixed with "+ " are included. Files prefixed with "- " are excluded.
#
# The order of included and excluded files matters! For instance, if a folder
# is excluded first, no subdirectory can be included anymore. Therefore,
# mention included files first. Then, mention excluded files.
#
# See section "FILTER RULES" of rsync manual for more details.


# Included Files

# TODO: This rules do not work properly!
+ output/***
+ pull_all.sh
- Repos/**

# Excluded Files

- .android
- .cache
...


I use the filter list in my script run_rsync.sh:



#!/bin/bash

date="$(date +%Y-%m-%d)"
hostname="$(hostname)"

# debug_mode="" # to disable debug mode
debug_mode="--list-only"

# Note: With trailing "/" at source directory, source directory is not created at destination.
rsync $debug_mode --archive --delete --human-readable --filter="merge $hostname.rsync.filters" --log-file=logfiles/$date-$hostname-home.log --verbose /home backup/


Unfortunately, the existing StackExchange threads have not solved my problems:



  • https://stackoverflow.com/questions/8270519/rsync-exclude-a-directory-but-include-a-subdirectory

  • Using Rsync include and exclude options to include directory and subdirectory but exlude files in subdirectory

What's going wrong here?



[Update] Here is an example how the home directory looks like and which files to keep and which files to ignore:



user@hostname:~$ tree /home/ | head
/home/
└── user
├── Desktop -> keep this
│   ├── file1 -> keep this
│   └── file2 -> keep this
├── Documents -> keep this
├── Repos
│   ├── pull_all.sh -> keep this
├── subdir1
│ ├── output -> keep this
├── subdir2
├── another_subdir
├── output -> keep this
├── subdir3 -> do not keep (because does not contain any "output")
├── file3 -> do not keep









share|improve this question
























  • Also take a look at Bash scripting and rsync: how to include just some folders and subfolders...

    – roaima
    Feb 27 at 8:28














2












2








2








I want to back up my /home directory with rsync. I have read rsync's man page and decided to use filter rules for this task.



What I would like to achieve: Exclude all files and directories in the Repos directory but keep all pull_all.sh files and output directories --- regardless where they are located within the Repos directory.



So far, I have ended up with following filter list, but this backs up only the pull_all.sh files but not the output directories:



# Files prefixed with "+ " are included. Files prefixed with "- " are excluded.
#
# The order of included and excluded files matters! For instance, if a folder
# is excluded first, no subdirectory can be included anymore. Therefore,
# mention included files first. Then, mention excluded files.
#
# See section "FILTER RULES" of rsync manual for more details.


# Included Files

# TODO: This rules do not work properly!
+ output/***
+ pull_all.sh
- Repos/**

# Excluded Files

- .android
- .cache
...


I use the filter list in my script run_rsync.sh:



#!/bin/bash

date="$(date +%Y-%m-%d)"
hostname="$(hostname)"

# debug_mode="" # to disable debug mode
debug_mode="--list-only"

# Note: With trailing "/" at source directory, source directory is not created at destination.
rsync $debug_mode --archive --delete --human-readable --filter="merge $hostname.rsync.filters" --log-file=logfiles/$date-$hostname-home.log --verbose /home backup/


Unfortunately, the existing StackExchange threads have not solved my problems:



  • https://stackoverflow.com/questions/8270519/rsync-exclude-a-directory-but-include-a-subdirectory

  • Using Rsync include and exclude options to include directory and subdirectory but exlude files in subdirectory

What's going wrong here?



[Update] Here is an example how the home directory looks like and which files to keep and which files to ignore:



user@hostname:~$ tree /home/ | head
/home/
└── user
├── Desktop -> keep this
│   ├── file1 -> keep this
│   └── file2 -> keep this
├── Documents -> keep this
├── Repos
│   ├── pull_all.sh -> keep this
├── subdir1
│ ├── output -> keep this
├── subdir2
├── another_subdir
├── output -> keep this
├── subdir3 -> do not keep (because does not contain any "output")
├── file3 -> do not keep









share|improve this question
















I want to back up my /home directory with rsync. I have read rsync's man page and decided to use filter rules for this task.



What I would like to achieve: Exclude all files and directories in the Repos directory but keep all pull_all.sh files and output directories --- regardless where they are located within the Repos directory.



So far, I have ended up with following filter list, but this backs up only the pull_all.sh files but not the output directories:



# Files prefixed with "+ " are included. Files prefixed with "- " are excluded.
#
# The order of included and excluded files matters! For instance, if a folder
# is excluded first, no subdirectory can be included anymore. Therefore,
# mention included files first. Then, mention excluded files.
#
# See section "FILTER RULES" of rsync manual for more details.


# Included Files

# TODO: This rules do not work properly!
+ output/***
+ pull_all.sh
- Repos/**

# Excluded Files

- .android
- .cache
...


I use the filter list in my script run_rsync.sh:



#!/bin/bash

date="$(date +%Y-%m-%d)"
hostname="$(hostname)"

# debug_mode="" # to disable debug mode
debug_mode="--list-only"

# Note: With trailing "/" at source directory, source directory is not created at destination.
rsync $debug_mode --archive --delete --human-readable --filter="merge $hostname.rsync.filters" --log-file=logfiles/$date-$hostname-home.log --verbose /home backup/


Unfortunately, the existing StackExchange threads have not solved my problems:



  • https://stackoverflow.com/questions/8270519/rsync-exclude-a-directory-but-include-a-subdirectory

  • Using Rsync include and exclude options to include directory and subdirectory but exlude files in subdirectory

What's going wrong here?



[Update] Here is an example how the home directory looks like and which files to keep and which files to ignore:



user@hostname:~$ tree /home/ | head
/home/
└── user
├── Desktop -> keep this
│   ├── file1 -> keep this
│   └── file2 -> keep this
├── Documents -> keep this
├── Repos
│   ├── pull_all.sh -> keep this
├── subdir1
│ ├── output -> keep this
├── subdir2
├── another_subdir
├── output -> keep this
├── subdir3 -> do not keep (because does not contain any "output")
├── file3 -> do not keep






linux rsync filter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 1 at 7:44







Schwefelsaeure

















asked Feb 27 at 7:59









SchwefelsaeureSchwefelsaeure

133




133












  • Also take a look at Bash scripting and rsync: how to include just some folders and subfolders...

    – roaima
    Feb 27 at 8:28


















  • Also take a look at Bash scripting and rsync: how to include just some folders and subfolders...

    – roaima
    Feb 27 at 8:28

















Also take a look at Bash scripting and rsync: how to include just some folders and subfolders...

– roaima
Feb 27 at 8:28






Also take a look at Bash scripting and rsync: how to include just some folders and subfolders...

– roaima
Feb 27 at 8:28











1 Answer
1






active

oldest

votes


















1














Slightly restating what I've interpreted as your requirements,



  • Include all pull_all.sh files regardless of where we find them

  • Include all output directories and their contents regardless of where we find them

  • Exclude the Repos directory, other than what we have already stated

  • Include everything else

This can be specified as follows



rsync --dry-run --prune-empty-dirs -av

--include 'pull_all.sh'
--include 'Repos/**/output/***'

--include '*/'

--exclude 'Repos/***'

/home backup/


Some notes



  • The --include '*/' is required so that rsync will consider heading down into the Repos directory tree (to look for pull_all.sh files), which would otherwise be excluded by the final --exclude statement.

  • The three different uses of * are different:


    • * matches anything except / characters


    • ** matches anything including / characters


    • dir/*** is a shortcut equivalent to specifying dir/ and dir/**.


  • The --prune-empty-dirs flag stops rsync creating empty directories, which is particularly important as we need to process the Repos directory tree looking for pull_all.sh and output items.

  • Remove --dry-run when you are happy with the results.





share|improve this answer

























  • I have updated my answer to address your revised question.

    – roaima
    Mar 1 at 12:07











  • Thanks a lot. :) The updated answer works like a charm. I guess the key was to add --include '*/'. Otherwise, rsync would not have traversed the Repos directory.

    – Schwefelsaeure
    Mar 4 at 6:33











  • Yes, that's right. I've added some notes that I hope make it clearer what's going on.

    – roaima
    Mar 4 at 9:32










Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f503270%2frsync-use-filters-to-exclude-top-level-directory-but-include-some-of-its-subdir%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Slightly restating what I've interpreted as your requirements,



  • Include all pull_all.sh files regardless of where we find them

  • Include all output directories and their contents regardless of where we find them

  • Exclude the Repos directory, other than what we have already stated

  • Include everything else

This can be specified as follows



rsync --dry-run --prune-empty-dirs -av

--include 'pull_all.sh'
--include 'Repos/**/output/***'

--include '*/'

--exclude 'Repos/***'

/home backup/


Some notes



  • The --include '*/' is required so that rsync will consider heading down into the Repos directory tree (to look for pull_all.sh files), which would otherwise be excluded by the final --exclude statement.

  • The three different uses of * are different:


    • * matches anything except / characters


    • ** matches anything including / characters


    • dir/*** is a shortcut equivalent to specifying dir/ and dir/**.


  • The --prune-empty-dirs flag stops rsync creating empty directories, which is particularly important as we need to process the Repos directory tree looking for pull_all.sh and output items.

  • Remove --dry-run when you are happy with the results.





share|improve this answer

























  • I have updated my answer to address your revised question.

    – roaima
    Mar 1 at 12:07











  • Thanks a lot. :) The updated answer works like a charm. I guess the key was to add --include '*/'. Otherwise, rsync would not have traversed the Repos directory.

    – Schwefelsaeure
    Mar 4 at 6:33











  • Yes, that's right. I've added some notes that I hope make it clearer what's going on.

    – roaima
    Mar 4 at 9:32















1














Slightly restating what I've interpreted as your requirements,



  • Include all pull_all.sh files regardless of where we find them

  • Include all output directories and their contents regardless of where we find them

  • Exclude the Repos directory, other than what we have already stated

  • Include everything else

This can be specified as follows



rsync --dry-run --prune-empty-dirs -av

--include 'pull_all.sh'
--include 'Repos/**/output/***'

--include '*/'

--exclude 'Repos/***'

/home backup/


Some notes



  • The --include '*/' is required so that rsync will consider heading down into the Repos directory tree (to look for pull_all.sh files), which would otherwise be excluded by the final --exclude statement.

  • The three different uses of * are different:


    • * matches anything except / characters


    • ** matches anything including / characters


    • dir/*** is a shortcut equivalent to specifying dir/ and dir/**.


  • The --prune-empty-dirs flag stops rsync creating empty directories, which is particularly important as we need to process the Repos directory tree looking for pull_all.sh and output items.

  • Remove --dry-run when you are happy with the results.





share|improve this answer

























  • I have updated my answer to address your revised question.

    – roaima
    Mar 1 at 12:07











  • Thanks a lot. :) The updated answer works like a charm. I guess the key was to add --include '*/'. Otherwise, rsync would not have traversed the Repos directory.

    – Schwefelsaeure
    Mar 4 at 6:33











  • Yes, that's right. I've added some notes that I hope make it clearer what's going on.

    – roaima
    Mar 4 at 9:32













1












1








1







Slightly restating what I've interpreted as your requirements,



  • Include all pull_all.sh files regardless of where we find them

  • Include all output directories and their contents regardless of where we find them

  • Exclude the Repos directory, other than what we have already stated

  • Include everything else

This can be specified as follows



rsync --dry-run --prune-empty-dirs -av

--include 'pull_all.sh'
--include 'Repos/**/output/***'

--include '*/'

--exclude 'Repos/***'

/home backup/


Some notes



  • The --include '*/' is required so that rsync will consider heading down into the Repos directory tree (to look for pull_all.sh files), which would otherwise be excluded by the final --exclude statement.

  • The three different uses of * are different:


    • * matches anything except / characters


    • ** matches anything including / characters


    • dir/*** is a shortcut equivalent to specifying dir/ and dir/**.


  • The --prune-empty-dirs flag stops rsync creating empty directories, which is particularly important as we need to process the Repos directory tree looking for pull_all.sh and output items.

  • Remove --dry-run when you are happy with the results.





share|improve this answer















Slightly restating what I've interpreted as your requirements,



  • Include all pull_all.sh files regardless of where we find them

  • Include all output directories and their contents regardless of where we find them

  • Exclude the Repos directory, other than what we have already stated

  • Include everything else

This can be specified as follows



rsync --dry-run --prune-empty-dirs -av

--include 'pull_all.sh'
--include 'Repos/**/output/***'

--include '*/'

--exclude 'Repos/***'

/home backup/


Some notes



  • The --include '*/' is required so that rsync will consider heading down into the Repos directory tree (to look for pull_all.sh files), which would otherwise be excluded by the final --exclude statement.

  • The three different uses of * are different:


    • * matches anything except / characters


    • ** matches anything including / characters


    • dir/*** is a shortcut equivalent to specifying dir/ and dir/**.


  • The --prune-empty-dirs flag stops rsync creating empty directories, which is particularly important as we need to process the Repos directory tree looking for pull_all.sh and output items.

  • Remove --dry-run when you are happy with the results.






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 4 at 9:31

























answered Feb 27 at 10:24









roaimaroaima

45.9k758124




45.9k758124












  • I have updated my answer to address your revised question.

    – roaima
    Mar 1 at 12:07











  • Thanks a lot. :) The updated answer works like a charm. I guess the key was to add --include '*/'. Otherwise, rsync would not have traversed the Repos directory.

    – Schwefelsaeure
    Mar 4 at 6:33











  • Yes, that's right. I've added some notes that I hope make it clearer what's going on.

    – roaima
    Mar 4 at 9:32

















  • I have updated my answer to address your revised question.

    – roaima
    Mar 1 at 12:07











  • Thanks a lot. :) The updated answer works like a charm. I guess the key was to add --include '*/'. Otherwise, rsync would not have traversed the Repos directory.

    – Schwefelsaeure
    Mar 4 at 6:33











  • Yes, that's right. I've added some notes that I hope make it clearer what's going on.

    – roaima
    Mar 4 at 9:32
















I have updated my answer to address your revised question.

– roaima
Mar 1 at 12:07





I have updated my answer to address your revised question.

– roaima
Mar 1 at 12:07













Thanks a lot. :) The updated answer works like a charm. I guess the key was to add --include '*/'. Otherwise, rsync would not have traversed the Repos directory.

– Schwefelsaeure
Mar 4 at 6:33





Thanks a lot. :) The updated answer works like a charm. I guess the key was to add --include '*/'. Otherwise, rsync would not have traversed the Repos directory.

– Schwefelsaeure
Mar 4 at 6:33













Yes, that's right. I've added some notes that I hope make it clearer what's going on.

– roaima
Mar 4 at 9:32





Yes, that's right. I've added some notes that I hope make it clearer what's going on.

– roaima
Mar 4 at 9:32

















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f503270%2frsync-use-filters-to-exclude-top-level-directory-but-include-some-of-its-subdir%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay