Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux Shell脚本压缩文件夹并将其复制到另一个装载点_Linux_Shell_Scripting - Fatal编程技术网

Linux Shell脚本压缩文件夹并将其复制到另一个装载点

Linux Shell脚本压缩文件夹并将其复制到另一个装载点,linux,shell,scripting,Linux,Shell,Scripting,我需要一个shell脚本来压缩.tar.gz目录/hana/shared/backup\u service/backups/30015/NDB/的内容,并将其复制到另一个文件夹:/hana/data/ 但是这些文件是根据日期和时间命名的。因此,第一次运行将创建bck_20180121000002,第二次运行将创建bck_201801220000001等。每个备份都将是一个完整的备份,包含该目录中的任何内容。以下是我多年来使用的内容: #!/bin/sh ## ==================

我需要一个shell脚本来压缩.tar.gz目录/hana/shared/backup\u service/backups/30015/NDB/的内容,并将其复制到另一个文件夹:/hana/data/


但是这些文件是根据日期和时间命名的。因此,第一次运行将创建bck_20180121000002,第二次运行将创建bck_201801220000001等。每个备份都将是一个完整的备份,包含该目录中的任何内容。

以下是我多年来使用的内容:

#!/bin/sh
## =====================================================
## backup sets: 'progs' (programs) or 'work'
## using "differential" technique (7 days per round)
## then copy to a mount point (USB)
##
## The mount point is defined in /etc/fstab as follows,
## which allow user to mount USB with disk label=BACKUP
## to the mount point /mnt/backup
##
## LABEL=BACKUP /mnt/backup auto user,noauto,noatime 0 0
##
## "Differential" technique:
## 1. find if there is a full backup within 7 days
## 2. if found, then backup only files that are changes since
##              the last full backup (diff backup)
## 3. if not, perform a full backup
## 4. to restore: tar xf the last full backup
##           and: tar xf the last diff backup
##
## Backups are encrypted using gnupg, placed in
## $HOME/BACKUPDIR
## Then check if the (USB) label=BACKUP exist, by checking
## if directory /mnt/backup/backup exists (it is a folder
## in that USB; ## if yes, then copy to that USB
##

## =====================================================

showUsage() {
    echo "`basename $0` progs | work [full]"
}

copyToOtherDisk() {
    [ -e "$1" ] || return
    if [ -b /dev/disk/by-label/BACKUP ]; then
        ## backup disk found
        local mounted=0
        if [ ! -d /mnt/backup/backup/ ]; then
            mount /mnt/backup && mounted=1
        fi
        if [ -d /mnt/backup/backup/ ]; then
            echo "cp -v $1 /mnt/backup/backup/."
            cp -v "$1" /mnt/backup/backup/.
            ls -la /mnt/backup/backup/
            sync
            df -h /mnt/backup
        fi
        [ $mounted = 1 ] && umount /mnt/backup
    fi
}


## =====================================================

timeStamp=`date +%y%m%d`

case "x$1" in
  xprogs)
    : ## details removed
    ;;
  xwork)
    if cd $HOME; then
        backupObj='folder1 folder2 file1 file2' ## detailed removed, replaced by samples
        ## find files modified within 7 days
        lastFullBackup=`find $HOME/BACKUPDIR/ -maxdepth 1 -type f -mtime -7 -name 'backup-work-*-full.tar.xz.gpg' | tail -n 1`
        if [ -z $lastFullBackup ] || [ "x$2" = "xfull" ]; then
            echo "Backup starts, full"
            echo "tar -cJf - $backupObj | gpg -c >$HOME/BACKUPDIR/backup-work-$timeStamp-full.tar.xz.gpg"
            tar -cJf - $backupObj | gpg -c >$HOME/BACKUPDIR/backup-work-$timeStamp-full.tar.xz.gpg
            copyToOtherDisk "$HOME/BACKUPDIR/backup-work-$timeStamp-full.tar.xz.gpg"
            echo "Backup ends"
        else
            echo "Backup starts, diff"
            echo "tar -N $lastFullBackup -cJf - $backupObj | gpg -c >$HOME/BACKUPDIR/backup-work-$timeStamp-diff.tar.xz.gpg"
            tar -N $lastFullBackup -cJf - $backupObj | gpg -c >$HOME/BACKUPDIR/backup-work-$timeStamp-diff.tar.xz.gpg
            copyToOtherDisk "$HOME/BACKUPDIR/backup-work-$timeStamp-diff.tar.xz.gpg"
            echo "Backup ends"
        fi
        cd -
    else
        echo "Error: $HOME"
    fi
    ;;
  *)
    showUsage
    ;;
esac

## vim:set ts=4:
## local variables:
## tab-width: 4
## end:

下面是一个日常使用的稍加修改的脚本:

    BackupName=$1
    today=`date +'%Y%m%d'`

help="Make backup in $startdir directory "
    usage="usage: $0 Backup-File-Name dir dir ..."
    if [  $# -lt 2 ]
    then
        echo $help
        echo $usage
        exit
    fi
    olddir=`pwd`
    tempdir=$$
    mkdir $tempdir
    shift 1
    # Each directory will be readed
    for dir in "$@"
    do
        #echo $dir
        directory=`dirname $dir`
        name=`basename $dir`

    cd $directory
    echo "Directory:$directory "
    # make tarball
    if [ -d $name ]
    then
            echo "Make tarball from $name"
            tar czvf $olddir/$tempdir/$name.$today.tar.gz $name
            mv $olddir/$tempdir/$name.$today.tar.gz $BackupName
    fi
done
cd $olddir
rmdir $tempdir
您必须启动脚本:

/Path_to_script/Script.sh /hana/data /hana/shared/backup_service/backups/30015/NDB/

这不是一个完整的解决方案,但应该在很大程度上有所帮助。我希望如此

你是在找人帮你写剧本吗?你试过什么?结果如何?收到了哪些错误?请尽快阅读该页面,并访问描述和的链接。感谢您的帮助:)这非常有帮助,但它不会在完成复制后退出脚本!也许我应该手动关闭它?还是应该在后台工作?请耐心等待!Scrript可以工作,但如果您想查看它的功能,请将选项“v”设置为tar命令。复制粘贴时我丢失了两行。我再次编辑了答案。