我的使用钥匙串的备份脚本有问题。通过命令行启动时,该脚本运行良好,但通过 cron 调用时无法按预期工作(不会将任何数据传输到远程主机)。两台服务器都运行 Ubuntu Server 14.04 LTS。我id_rsa
使用空密码,无需任何密码即可连接到远程主机。
Cron任务:
* * * * * bash /home/kris/sync.sh
脚本:
#!/bin/bash
source $HOME/.keychain/${HOSTNAME}-sh
rsync -avz --delete --log-file=/home/kris/rsync.log /var/www/owncloud/data/ [email protected]:/var/www/owncloud/data/
输出:
/home/kris/sync.sh: 2: /home/kris/sync.sh: source: not found
Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,password).
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at io.c(226) [sender=3.1.0]
。轮廓 :
/usr/bin/keychain $HOME/.ssh/id_rsa
source $HOME/.keychain/${HOSTNAME}-sh
答案1
我编写了以下脚本,连接到我的 PI ownclod 服务器并将其备份到我的笔记本电脑上。该脚本运行正常。您可以看看它是否对您有帮助:
#!/bin/bash
echo "[`date +"%y-%m-%d %H:%I:%S"`] Sync started";
CURRENT_DATE=`date +%y-%m-%d`
BACKUP_DIR="/home/radoslav/Backups"
CURRENT_BACKUP_DIR="$BACKUP_DIR/backup-$CURRENT_DATE"
LOG_FILE="$BACKUP_DIR/backup-$CURRENT_DATE.log"
if [ ! -d "$CURRENT_BACKUP_DIR" ]; then
mkdir "$CURRENT_BACKUP_DIR"
fi
if [ ! -f "$LOG_FILE" ]; then
touch "$LOG_FILE"
fi
rsync -aAXvzlxH --exclude='/dev' --exclude='/proc' --exclude='/sys' --exclude='/lost+found' -e "ssh" --rsync-path="sudo rsync" [email protected]:/ "$CURRENT_BACKUP_DIR" >"$LOG_FILE" 2>&1
tar -czvf "$BACKUP_DIR/backup-$CURRENT_DATE.tar.gz" "$CURRENT_BACKUP_DIR"
rm -rf "$CURRENT_BACKUP_DIR"
echo "[`date +"%y-%m-%d %H:%I:%S"`] Sync ended";
笔记我在 rsync 命令中使用它--rsync-path="sudo rsync"
。此外,corn 是在哪个用户下运行的?是在 root 用户下还是在生成 ssh 密钥的普通用户下?