21

I want to backup my Ubuntu system and I have two partitions to care: One is /, another is /home.

I'll likely want to backup /. This is not even that big so I can carry it on my SD card, while being responsive enough for making me want to have a image of it (that's why I'm trying to use rsync at this moment).

And about /home, it has many subdirectories that I do not care about much so I'll not likely include them, but I want to take care about files on there, such like .bash_history, .bashrc, .face, and so on.

So I want to exclude all subdirectories while including files in /home. How can I achieve that?

--exclude "*/" wasn't working. "/*/", "/**/", --include "*" --exclude */ doesn't show me what I want. at least it copied the source folder, without copying anything inside it.

Maythux
  • 84,289
Dinir
  • 335

4 Answers4

20

Try this command:

rsync -a -f"- */" -f"+ *" /home/user/ destination/

man rsync

-f, --filter=RULE           add a file-filtering RULE

The rule to include all files* and exclude the dirs */

Another approach to use the regular copy cp

cp /home/usr/* /destination

you can get rid of the errors about dirs using redirection

cp /home/usr/* /destination 2>/dev/null

This will only copies the files inside your home without the directories

Maythux
  • 84,289
  • /proc has a virtual unreal filesystem, you should exclude it from backup, also you should exclude the /tmp and /run – Maythux Jun 04 '15 at 08:40
  • My original command for backing up things was like this: rsync -r -t -p -o -g -v --progress --delete --modify-window=1 -l -H -b -s --exclude home/dinir/ --exclude media/ --exclude proc/kcore / /home/dinir/Temporal Site for Storing Backups – Dinir Jun 04 '15 at 08:40
  • /proc, /tmp, and /run, Check! Anyway, as you see in my comment, I was to backup entire root file system excluding home directory with little exceptions. Would putting the -f" */" -f"+ *" in my code be safe? Maybe it would be like -f"- home/dinir/*/" -f"+ home/dinir/*"? – Dinir Jun 04 '15 at 08:41
  • you should use it like so -f"- home/dinir/*/" -f"+ home/dinir/*" – Maythux Jun 04 '15 at 08:50
  • but I advice use to use cp for your home, it's much easier – Maythux Jun 04 '15 at 08:50
  • I don't sure if I want to backup my home partition. What I really am scared about is losing all the configurations I've made. If using cp is also much easier for root directory, then would it be meaning just overwriting whole system with a past well-worked one is enough to restore my system? – Dinir Jun 04 '15 at 08:54
  • I advise you to use cp for your home files only not for the /. rsync there better, but since you just need some files from home it's easiest to use cp – Maythux Jun 04 '15 at 08:56
  • http://askubuntu.com/tour – Maythux Jun 04 '15 at 10:51
5

If you want to solve this with --include and --exclude parameters, you should change the order of them: first exclude what you want, then include everything. I do it usually with this command:

rsync -vazhP path/to/source path/to/dest --exclude '*/*/' --include '*'
Mattia72
  • 161
4

Try this:

rsync -r --exclude='*/' source/ destination/

Source: https://lists.samba.org/archive/rsync/2008-September/021746.html

3

By default, rsync does not copy directories. Perhaps you are using the -a flag. If this is the case, notice that according to the man page, -a is equivalent to -rlptgoD, so if you don't want to recurse, just use -lptgoD.

Brian Moths
  • 356
  • 2
  • 8