英文:
Bash Backup script: Permission denied
问题
I have written a backup script for my linux machine that encrypts the tarball and sends it to a remote server with scp with configured ssh key pairs, but it gives me the following error when i run it: ./backup_script.sh: line 27: /home/zero/Backup/backup_20230731_1251.tar.gz.gpg: Permission denied
#!/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 over ssh to the remote server
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
The backup is made successfully, and the encryption works perfectly but it won't send to the remote server. I made the script in the root profile and I am running it as root. Here are the file permissions:
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
My question is: How do I make it send to the remote server?
I have tried changing file permissions but I have had no luck.
英文:
I have written a backup script for my linux machine that encrypts the tarball and sends it to a remote server with scp with configured ssh key pairs, but it gives me the following error when i run it: ./backup_script.sh: line 27: /home/zero/Backup/backup_20230731_1251.tar.gz.gpg: Permission denied
#!/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
The backup is made successfully, and the encryption works perfectly but it wont send to the remote server. I made the script in the root profile and i am running it as root. Here are the file permissions:
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
My question is: How do i make it send to the remote server?
I have tried changing file permissions but i have had no luck.
答案1
得分: 2
这是我最终想出的修复方法。感谢这个答案https://stackoverflow.com/a/76803519/20177543提到它没有执行权限
# 检查是否找到文件
if [ -n "$filename" ]; then
# 如果找到文件,将其通过SSH发送到远程服务器
chmod +x $filename
scp $filename root@178.79.xxx.xxx:/root/Backup
if [ $? -eq 0 ]; then
echo "$(date): 备份成功" >> /home/zero/Backup/backup.log
# 一旦文件被发送到远程服务器,就删除文件
rm -f $filename
else
echo "$(date): 发送备份到远程服务器时发生错误" >> /home/zero/Backup/backup.log
fi
else
# 如果未找到文件,则显示错误消息
echo "$(date): 未找到加密文件" >> /home/zero/Backup/backup.log
fi
英文:
This is the fix i have come up with in the end. Thanks to this answer https://stackoverflow.com/a/76803519/20177543 mentioning that it doesn't have execute permissions
# 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
答案2
得分: 1
你忘了在这里使用命令替换语法,它试图将$filename
作为命令执行。
我假设在这一点上filename= /home/zero/Backup/backup_20230731_1251.tar.gz.gpg
,并且它没有_执行_权限。
英文:
send=scp $filename root@178.79.148.186:/root/Backup
You forgot the command substitution syntax here, and it's trying to execute $filename
as a command.
I presume at this point that filename= /home/zero/Backup/backup_20230731_1251.tar.gz.gpg
and it doesn't have execute permissions.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论