linuxtarback

Exlude files when I backup linux tar


I'm trying to do a backup in linux I have tried this :

tar --exclude={"logs", "volumes_docker", "Backup"} -zcvf backup1.tar.gz /home/debian

but when I check backup1.tar.gz I find data from yje folders mentioned above.

Could you help please ?


Solution

  • Each directory to be excluded should ideally have its own --exclude flag with the full path specified. E.g.:

    tar -zcvf backup1.tar.gz --exclude='/home/debian/logs' --exclude='/home/debian/volumes_docker' --exclude='/home/debian/Backup' /home/debian
    

    Ensure that the --exclude path doesn't contain any symlinked directories (this has bitten me before). When tar encounters a symlinked directory specified by --exclude, it will exclude the symlink itself, not the target directory.

    Once you have that working, for a more concise approach, you can use brace expansion in the shell (as long as your shell supports it):

    tar -zcvf backup1.tar.gz --exclude=/home/debian/{logs,volumes_docker,Backup} /home/debian
    

    Here, --exclude=/home/debian/{logs,volumes_docker,Backup} is expanded by the shell before the command is executed, effectively making the command equivalent to repeating the --exclude option for each dir.