Created
          April 11, 2016 01:26 
        
      - 
      
- 
        Save steve-todorov/5d31ad846eabdf05925d3fa3962422b4 to your computer and use it in GitHub Desktop. 
    Create a backup of a file and try NOT to overwrite any previous backups. Also make sure not to create unnecessary backups in case the files are identical.
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | backup() { | |
| f=$1 | |
| bkp=$f.bkp | |
| index=1 | |
| while true; do | |
| # Make sure NOT TO OVERWRITE a bkp file!!! | |
| if [[ -f "$bkp.$index" ]]; then | |
| prevBkp="$bkp.$index" | |
| index=$((index+1)) | |
| continue | |
| fi | |
| bkp=$bkp.$index | |
| copy=y | |
| # Avoid making unnecessary backups. | |
| if [[ $prevBkp != "" ]]; then | |
| file1=`md5sum -b $f | cut -f 1 -d " "` | |
| file2=`md5sum -b $prevBkp | cut -f 1 -d " "` | |
| if [[ "$file1" == "$file2" ]]; then | |
| copy=n | |
| fi | |
| fi | |
| if [[ $copy == "y" ]]; then | |
| cp $f $bkp | |
| fi | |
| break | |
| done | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment