Learn about Centmin Mod LEMP Stack today
Become a Member

Exclude a folder on tar

Discussion in 'System Administration' started by pamamolf, Nov 17, 2014.

  1. pamamolf

    pamamolf Well-Known Member

    4,022
    421
    83
    May 31, 2014
    Ratings:
    +816
    Local Time:
    5:29 PM
    Nginx-1.17.x
    MariaDB 10.3.x
    Hi
    I want to backup the contents of the folder:
    Code:
    /home/username/public_html/
    Inside public_html is a folder named uploads that i want to exclude from tar (completely exclude this folder and all files in it and all subfolders in it)

    So i cd here:
    Code:
    cd /home/username
    Then i found from Google many recommendations like:
    Code:
    tar -cpvzf /home/username/sitefiles.tgz  --exclude=/home/username/public_html/uploads public_html
    or
    Code:
    tar -cpvzf /home/username/sitefiles.tgz public_html --exclude=/home/username/public_html/uploads
    or
    Code:
    tar --exclude=/home/username/public_html/uploads -cpvzf /home/username/sitefiles.tgz public_html 
    or
    Code:
    tar -cpvzf /home/username/sitefiles.tgz public_html --exclude=./home/username/public_html/uploads
    or
    Code:
    tar -cpvzf /home/username/sitefiles.tgz public_html --exclude="/home/username/public_html/uploads"
    No way to get this folder excluded as i can see from ssh when i run the commands.... :(


    Any ideas?
     
  2. rdan

    rdan Well-Known Member

    5,419
    1,386
    113
    May 25, 2014
    Ratings:
    +2,167
    Local Time:
    10:29 PM
    Mainline
    10.2
    --exclude='*YOURFOLDERNAME*'

    So to exclude uploads directory:
    --exclude='*uploads*'
     
  3. pamamolf

    pamamolf Well-Known Member

    4,022
    421
    83
    May 31, 2014
    Ratings:
    +816
    Local Time:
    5:29 PM
    Nginx-1.17.x
    MariaDB 10.3.x
    Like this without giving a path?

    Code:
    tar -cpvzf /home/username/sitefiles.tgz public_html --exclude='*uploads*'
     
  4. rdan

    rdan Well-Known Member

    5,419
    1,386
    113
    May 25, 2014
    Ratings:
    +2,167
    Local Time:
    10:29 PM
    Mainline
    10.2
    Like this:
    Code:
    tar -cf "/home/username/sitefiles.tgz" "/home/username/public_html" --exclude='*uploads*'
     
  5. pamamolf

    pamamolf Well-Known Member

    4,022
    421
    83
    May 31, 2014
    Ratings:
    +816
    Local Time:
    5:29 PM
    Nginx-1.17.x
    MariaDB 10.3.x
    Working thanks :)
     
    Last edited: Nov 18, 2014
  6. ndha

    ndha Member

    82
    10
    8
    Sep 28, 2014
    Ratings:
    +29
    Local Time:
    9:29 PM
    Latest
    10
    Hi @pamamolf or @RoldanLT or @eva2000
    is this still work till now??
    Cause i tried it won't work..

    Also how about if i want to exclude multiple domain folders with subfolders on it from tar??
    CD into /home/nginx/domains
    There is :
    domain1
    domain2
    domain3
    domain4
    domain5
    I want to exclude domain1, domain2, domain3, domain4 and all sub folders from tar.
    What command do i must use??
    Thanks for any kindly help..
     
  7. eva2000

    eva2000 Administrator Staff Member

    50,479
    11,663
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,088
    Local Time:
    12:29 AM
    Nginx 1.25.x
    MariaDB 10.x
    should still work provided each exclude has it's own --exclude= and is relative to directory

    or use --exclude-from to point to a file path with file listing all relative excluded files/directories
    but better yet if you just want domain5, just tar that directory instead https://linux.die.net/man/1/tar

    if unsure, do the suggested step at https://community.centminmod.com/threads/guide-to-learning-more-about-centmin-mod.10838/ in setting up a test centmin mod server with test data or duplicate of your live data, and play with the tar command until you figure it out. This way you learn without fear of messing up as you are doing it on test server.
     
  8. ndha

    ndha Member

    82
    10
    8
    Sep 28, 2014
    Ratings:
    +29
    Local Time:
    9:29 PM
    Latest
    10
    Hi @eva2000 thanks for reply,
    yes i'd already test various command in test server..sadly nothing work..
    i use : --exclude="domain1" --exclude="/domain2" etc also using --exclude-from="excludedomain.txt" and put also exclude folder on it but still not excluding..
    Command 1 : --exclude="domain1" = domain1 folder still include but not subfolders on it.
    Command 2 : --exclude="/domain1" = domain1 folder still include also subfolders on it.
    Command 3 : --exclude="/domain1/" = domain1 folder still include also subfolders on it.
    Command 4 : --exclude="/domain1/*" = domain1 folder still include also subfolders on it.

    In simple words i want exclude 3 domains (including subfolders to not tar) if i had 5 or more domains within /home/nginx/domains directory..
    i still don't get what's exclude command work for it..Already Google Fu about 2 hours but still hopeless..lol..
     
  9. eva2000

    eva2000 Administrator Staff Member

    50,479
    11,663
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,088
    Local Time:
    12:29 AM
    Nginx 1.25.x
    MariaDB 10.x
    I suggest keep practising as there's no better feeling that conquering some obstacle and nailing a new piece of system admin know how ;) :)