ok, if you are like me, i do backups all the time, and the best way to backup my files is to create tarballs. i have a linux server and i use it to host my wallpaper websites. as an admin, i have to backup my files frequently. today i came accross a problem. i wanted to create a backup file but i didnt want to include this huge file so i wanted to exclude it from my backups. but then i noticed that i had another directory i didnt want to be included in my backups. so now i have multiple (two or more) directories that i wanted to avoid including in my tar file. excluding these files would make my backups smaller and thus saving me space.
so the questions is, how do you tar a backup without including these two directories?
well the answer is easy, when you are done reading this short tutorial, you will learn how you can exclude a file or a directory in your tars.
ok, here is my example,
1. - i want to keep the same permission on each file and directory (-pczf)
2. - the name of my backup tar file is called mywebsites.tar.gz
3. - i wanted to backup my entire /var/www directory
4. - the two directories i did not want to put in my tar backup are:
- /var/www/mydomain/images/
- /var/www/otherdomain/pictures/
now that i have all these elements, i can construck the shell command im going to use in my linux server. so here is the command
tar -pczf mywebsites.tar.gz /var/www/ --exclude "/var/www/mydomain/images/" --exclude "/var/www/otherdomain/pictures"
so basically if you want to exclude a file that you dont want to be included in your tar, just use the --exclude option. easy huh
thats it
hope this helps
Comments and replies About how to exclude a directory in a tar linux command
wallpaperama:
for zip, you use the -x option instead of --exclude
wallpaperama:
ok here is a tutorial you can follow. login as root and then follow these steps:
1. create some test files in your root directory for us to play with
mkdir /root/tartest
mkdir /root/tartest/mydirectory
mkdir /root/tartest/mydirectory/avoid
mkdir /root/tartest/mydirectory/not_tar
mkdir /root/tartest/mydirectory/tarred
2. once you have create our test directories, go to our test directory:
cd /root/tartest
3. we just created three directories, our of all these three, we are only going to tar the directory called tarred/ and leave out avoid/ and not_tar/ so you send this command to do that:
tar -pczf mydirectory.tar.gz mydirectory/ --exclude "mydirectory/not_tar" --exclude "mydirectory/avoid"
4. now send the list command to see the new tar file you just created
ls
5.. now we just rename our original mydirectory/ to mydirectory-old
mv mydirectory mydirectory-old
6. now we untar the files
tar xvfz mydirectory.tar.gz
7. you will only see that the directory called tarred/ was the only one included in our tar. this is the output:
[root@host tartest]# tar xvfz mydirectory.tar.gz
mydirectory/
mydirectory/tarred/
[root@host tartest]#
8. these commands are if you want to try again from step three:
rm -rf mydirectory
rm -rf rm -rf mydirectory.tar.gz
mv mydirectory-old mydirectory
tzwil:
thank you i wanted to know how i can exclude a directory or a file from the comannd and your tutorial helped in in excluding a file that i didnt want to include when i send the linux shell command it didnt included the .sql file i didnt want to be packaged which is the exception
ir0nwarez:
It works perfectly to me!
Thanks for your tutorial