Find files and tar them (with spaces)

后端 未结 10 1851
北海茫月
北海茫月 2020-11-29 15:35

Alright, so simple problem here. I\'m working on a simple back up code. It works fine except if the files have spaces in them. This is how I\'m finding files and adding th

10条回答
  •  再見小時候
    2020-11-29 16:21

    The best solution seem to be to create a file list and then archive files because you can use other sources and do something else with the list.

    For example this allows using the list to calculate size of the files being archived:

    #!/bin/sh
    
    backupFileName="backup-big-$(date +"%Y%m%d-%H%M")"
    backupRoot="/var/www"
    backupOutPath=""
    
    archivePath=$backupOutPath$backupFileName.tar.gz
    listOfFilesPath=$backupOutPath$backupFileName.filelist
    
    #
    # Make a list of files/directories to archive
    #
    echo "" > $listOfFilesPath
    echo "${backupRoot}/uploads" >> $listOfFilesPath
    echo "${backupRoot}/extra/user/data" >> $listOfFilesPath
    find "${backupRoot}/drupal_root/sites/" -name "files" -type d >> $listOfFilesPath
    
    #
    # Size calculation
    #
    sizeForProgress=`
    cat $listOfFilesPath | while read nextFile;do
        if [ ! -z "$nextFile" ]; then
            du -sb "$nextFile"
        fi
    done | awk '{size+=$1} END {print size}'
    `
    
    #
    # Archive with progress
    #
    ## simple with dump of all files currently archived
    #tar -czvf $archivePath -T $listOfFilesPath
    ## progress bar
    sizeForShow=$(($sizeForProgress/1024/1024))
    echo -e "\nRunning backup [source files are $sizeForShow MiB]\n"
    tar -cPp -T $listOfFilesPath | pv -s $sizeForProgress | gzip > $archivePath
    

提交回复
热议问题