Find the answer to your Linux question:
Results 1 to 4 of 4
I have a fairly simple backup script that I run every Sunday morning. The problem is that some of the directories that I would like to back up have some ...
  1. #1
    Just Joined!
    Join Date
    Oct 2006
    Posts
    42

    A Script that Tars Files Under a Certain Size

    I have a fairly simple backup script that I run every Sunday morning. The problem is that some of the directories that I would like to back up have some very large files that I don't want to back up. Does anyone have an idea how I might have the script tar all files in a directory under a certain size?

    I'll paste in the contents of my backup script so you can see what it does:

    Code:
    #!/bin/bash
    
    config_dir=/etc/backups/
    tdy=`date +%y%m%d`
    days_to_keep=112
    
    get_file_names ()
    	{
    	new_file=$file_name"_"$tdy.tgz
    	if [ $(ls $file_name"_"* | wc -w) != 0 ]
    	then
    		old_file_exists=1
    		last_file=`ls -r $file_name"_"* | sed 1q`
    	fi
    	}
    
    compare_files ()
    	{
    	if [ -n "$old_file_exists" ]
    	then
    		old_file_exists=""
    		cmp -i 512 -s $last_file $new_file
    		if [ $? = "0" ]
    		then
    			rm $new_file
    		fi
    	fi
    	}
    
    cd /var/backups/server_backups
    
    find . -mtime +$days_to_keep -exec rm {} \;
    
    exec < $config_dir"backup_files"
    while read file_name files ; do
    	get_file_names
    	tar --exclude=*cache --exclude=*compiled --exclude=*Temp -czf $new_file $files 2>&1 | grep -v "Removing leading"
    	compare_files
    done
    Basically it reads a configuration file I have that lists the name that I want to give the backup up file, and the directory or file that I want back up, one name and file per line, then tars them up, excluding temp directories and caches, and then compares the sizes of the last backup with the size of the new backup and if they are the same it deletes the new backup (this isn't a perfect way to do it I know, but I can't think of any other reasonably simple way to do it).

    What I would like to do is have the tar command exlude all files above a certain size.

  2. #2
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    Use "find -size" to select or deselect files above or below a certain size.

  3. #3
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    As said above, find -size can list these files you need. Then you can, for example, pipe that list into xargs to tar them or whatever.

  4. #4
    Just Joined!
    Join Date
    Oct 2006
    Posts
    42
    Thanks for the responses. I will do some googling on xargs...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...