rsync: Use filters to exclude top-level directory but include some of its subdirectories
Clash Royale CLAN TAG#URR8PPP
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
add a comment |
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
Also take a look at Bash scripting and rsync: how to include just some folders and subfolders...
– roaima
Feb 27 at 8:28
add a comment |
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
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
linux rsync filter
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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 thatrsync
will consider heading down into theRepos
directory tree (to look forpull_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/
charactersdir/***
is a shortcut equivalent to specifyingdir/
anddir/**
.
- The
--prune-empty-dirs
flag stopsrsync
creating empty directories, which is particularly important as we need to process theRepos
directory tree looking forpull_all.sh
andoutput
items. - Remove
--dry-run
when you are happy with the results.
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 theRepos
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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 thatrsync
will consider heading down into theRepos
directory tree (to look forpull_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/
charactersdir/***
is a shortcut equivalent to specifyingdir/
anddir/**
.
- The
--prune-empty-dirs
flag stopsrsync
creating empty directories, which is particularly important as we need to process theRepos
directory tree looking forpull_all.sh
andoutput
items. - Remove
--dry-run
when you are happy with the results.
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 theRepos
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
add a comment |
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 thatrsync
will consider heading down into theRepos
directory tree (to look forpull_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/
charactersdir/***
is a shortcut equivalent to specifyingdir/
anddir/**
.
- The
--prune-empty-dirs
flag stopsrsync
creating empty directories, which is particularly important as we need to process theRepos
directory tree looking forpull_all.sh
andoutput
items. - Remove
--dry-run
when you are happy with the results.
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 theRepos
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
add a comment |
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 thatrsync
will consider heading down into theRepos
directory tree (to look forpull_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/
charactersdir/***
is a shortcut equivalent to specifyingdir/
anddir/**
.
- The
--prune-empty-dirs
flag stopsrsync
creating empty directories, which is particularly important as we need to process theRepos
directory tree looking forpull_all.sh
andoutput
items. - Remove
--dry-run
when you are happy with the results.
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 thatrsync
will consider heading down into theRepos
directory tree (to look forpull_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/
charactersdir/***
is a shortcut equivalent to specifyingdir/
anddir/**
.
- The
--prune-empty-dirs
flag stopsrsync
creating empty directories, which is particularly important as we need to process theRepos
directory tree looking forpull_all.sh
andoutput
items. - Remove
--dry-run
when you are happy with the results.
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 theRepos
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
add a comment |
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 theRepos
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Also take a look at Bash scripting and rsync: how to include just some folders and subfolders...
– roaima
Feb 27 at 8:28