shell Bash Backup脚本:拒绝许可

lyr7nygr  于 2023-08-07  发布在  Shell
关注(0)|答案(2)|浏览(130)

我已经为我的linux机器写了一个备份脚本,它加密tarball并将其发送到一个远程服务器,其中包含配置的ssh密钥对的scp,但是当我运行它时,它给了我以下错误:第一个月

#!/bin/bash

# Define the date format for the filename
datetime=$(date +"%Y%m%d_%H%M")

# Define the source directory and the backup directory
src_dir="/var/www/html" # this is the directory that we will back up
backup_dir="/home/zero/Backup" # This is where we will temporarily store the encrypted backup data

# Create a tarball of the source directory
tar -czf $backup_dir/backup_$datetime.tar.gz $src_dir

# Read the password from a secure file
password=$(cat /home/zero/Backup/pwd)

# Encrypt the tarball using openssl
gpg --pinentry-mode=loopback --passphrase $password --symmetric --cipher-algo AES256 --output $backup_dir/backup_$datetime.tar.gz.gpg $backup_dir/backup_$datetime.tar.gz

rm $backup_dir/backup_$datetime.tar.gz

# Get the .gpg filename 
filename=$(find /home/zero/Backup -type f -name "*.gpg" -print -quit)

# Check if a file was found
if [ -n "$filename" ]; then
    # if file is found send it ove ssh to the remote server
    send=scp $filename root@178.79.148.186:/root/Backup
        echo "$(date): Backup Successful" >> /home/zero/Backup/backup.log
    # remove file once it has been sent to remote server
    rm -f $filename
else
    # if file is not found display error msg 
    echo "$(date): No encrypted file found" >> /home/zero/Backup/backup.log
fi

字符串
备份成功,加密工作完美,但它不会发送到远程服务器。我在根配置文件的脚本,我运行它作为根.以下是文件权限:

drwxr-xr-x  2 zero zero  4096 Jul 31 12:51 .
drwx------ 37 zero zero  4096 Jul 31 12:40 ..
-rw-r--r--  1 root root  3578 Jul 31 12:50 backup_20230731_1250.tar.gz.gpg
-rw-r--r--  1 root root  3578 Jul 31 12:51 backup_20230731_1251.tar.gz.gpg
-rw-r--r--  1 zero zero    96 Jul 31 12:51 backup.log
-rwx------  1 root root  1465 Jul 31 12:51 backup_script.sh
-rw-------  1 root root     5 Jul 29 04:13 pwd


我的问题是:如何将其发送到远程服务器?
我试过更改文件权限,但我没有运气。

k3bvogb1

k3bvogb11#

这是我最终想出的解决办法。感谢这个答案https://stackoverflow.com/a/76803519/20177543提到它没有执行权限

# Check if a file was found
if [ -n "$filename" ]; then
    # if file is found send it over ssh to the remote server
    chmod +x $filename
    scp $filename root@178.79.xxx.xxx:/root/Backup
    if [ $? -eq 0 ]; then
        echo "$(date): Backup Successful" >> /home/zero/Backup/backup.log
        # remove file once it has been sent to remote server
        rm -f $filename
    else
        echo "$(date): Error occurred while sending backup to remote server" >> /home/zero/Backup/backup.log
    fi
else
    # if file is not found display error msg 
    echo "$(date): No encrypted file found" >> /home/zero/Backup/backup.log
fi

字符串

rbl8hiat

rbl8hiat2#

send=scp $filename root@178.79.148.186:/root/Backup

字符串
您忘记了这里的命令替换语法,它试图将$filename作为命令执行。我假设在这一点上,filename= /home/zero/Backup/backup_20230731_1251.tar.gz.gpg和它没有 * 执行 * 权限。

相关问题