dimanche 1 mars 2015

Gzip file but excluding some directories|files and also append current date


I'm looking for a way to create a .tar.gz file from a directory but I have some doubts. I'm know that the command for create the file is:



tar -zcvf /path/to/compressed/file.tar.gz /path/to/compress


And then I will get a file.tar.gz but this keep the path to the original sources. So this is what I'm looking for:



  • Should be compress as .tar.gz

  • If it's possible shouldn't keep the path to the compressed directory I mean I only needs the compress directory and its content .If I uncompress the file under Windows for example I'll get /path[folder]/to[folder]/compress[folder+content] and I just want the latest


  • If it's possible can I ommit some directories? Example I like to compress app/ folder and its content is:



    drwxrwxr-x 6 ubuntu ubuntu 4096 Feb 24 14:48 ./
    drwxr-xr-x 10 ubuntu ubuntu 4096 Feb 26 22:33 ../
    -rw-rw-r-- 1 ubuntu ubuntu 141 Jan 29 06:07 AppCache.php
    -rw-rw-r-- 1 ubuntu ubuntu 2040 Feb 24 14:48 AppKernel.php
    -rw-rw-r-- 1 ubuntu ubuntu 267 Jan 29 06:07 autoload.php
    -rw-r--r-- 1 root root 94101 Feb 19 21:09 bootstrap.php.cache
    drwxrwxrwx 4 ubuntu ubuntu 4096 Feb 25 16:44 cache/
    -rw-rw-r-- 1 ubuntu ubuntu 3958 Feb 24 14:48 check.php
    drwxrwxr-x 2 ubuntu ubuntu 4096 Feb 24 14:48 config/
    -rwxrwxr-x 1 ubuntu ubuntu 867 Jan 29 06:07 console*
    -rw-rw-r-- 1 ubuntu ubuntu 6148 Jan 29 06:07 .DS_Store
    -rw-rw-r-- 1 ubuntu ubuntu 143 Jan 29 06:07 .htaccess
    drwxrwxrwx 2 ubuntu ubuntu 4096 Feb 24 14:48 logs/
    -rw-rw-r-- 1 ubuntu ubuntu 1118 Jan 29 06:07 phpunit.xml.dist
    drwxrwxr-x 5 ubuntu ubuntu 4096 Jan 29 06:07 Resources/
    -rw-rw-r-- 1 ubuntu ubuntu 30404 Feb 24 14:48 SymfonyRequirements.php


    But I want to leave out cache/, logs/ directories and bootstrap.php.cache file, how? Is that possible?




  • I need to append the current date (DD/MM/YYYY-H:M:S) to the file name, how?




Can I get some advise or help on this? I'm planning to add this to a Bash script so it will works as a bash script and not as a command line


Update: testing inside the script


Following @Ariel suggestion I have added this line to a bash script:



read -e -p "Enter the directory to compress: " -i "/var/www/html/" directory
read -e -p "Enter the filename: " filename
FILENAME = "$filename-`date +%d-%m-%Y-%X`.tgz"

cd "$directory"
tar -zcvf /home/$FILENAME --exclude cache --exclude logs --exclude bootstrap.php.cache --exclude composer.lock --exclude vendor


But I'm getting this error:



Enter the directory to compress: /var/www/html/lynxoft/apps/checkengine/
Enter the filename: checkengine
/usr/local/bin/script-task: line 109: FILENAME: command not found
tar: Cowardly refusing to create an empty archive


Why FILENAME is treat as a command and not as a var as docs says?


Update 2: still compressing the whole path


I have fixed some issues thanks to users comments and lines on the script looks likes this:



read -e -p "Enter the directory to compress: " -i "/var/www/html/" directory
read -e -p "Enter the filename: " filename
FILENAME="$filename"-$(date +%d-%m-%Y-%T).tgz

cd "$directory/.."
tar -zcvf /home/$FILENAME "$directory" --exclude="*cache*" --exclude=logs --exclude=bootstrap.php.cache --exclude=composer.lock --exclude=vendor --exclude=.git


The only remaining issue is that compressed file still having the whole path and not just the end directory. For example:



Enter the directory to compress: /var/www/html/lynxoft/apps/checkengine/
Enter the filename: file


That result in file-28-02-2015-17:44:32.tgz but content inside the compressed file still having the whole path /var/www/html/lynxoft/apps/checkengine/, why?



Aucun commentaire:

Enregistrer un commentaire