我制作了一个脚本,以运行从本地计算机到远程服务器的备份。但是,运行此脚本时出现错误:
/etc/run_backup: 173: /etc/run_backup: Syntax error: Unterminated quoted string
对于我的一生,我无法理解为什么会收到此错误。下面是脚本(带有一些修改):
#!/bin/sh
#
# this script must be run as root on machine with folders
# to be backed up.
#
# This script will attempt to back up several folders
# to a remote server which must have passwordless ssh
# access set up for the root users on the machines. If
# this is inaccessible a backup will be attempted in some
# local storage, e.g. a usb drive. Locations are defined
# below
set -ex
##### CONFIG VARIABLES #####
# backup server location
backup_server=<redacted>
# name of top level folder used to contain all backups on either
# the remote server or local backup location
backup_folder=backups
# the root folder where $backup_folder will be placed on the
# remote machine
remote_backup_storage_root=/mnt/reos-storage-2
# the root folder where $backup_folder will be placed on
# local storage if the remote machine is not available
local_backup_storage_root=/media/usbflash0
# the directory wher the results of the backup attempt by rdiff-backup
# will be placed for each folder backup, and also the where the backup
# log file generated by this script will be placed
error_report_dir=/home/pi/rdiff-backup-errors
# email to send any messages to. An email will be sent on success
# and under certain error conditions
email_receiver=<redacted>
# the location of the nextcloud data folder to backup
nc_data_dir=/opt/nextcloud-data
# root directory of the nextcloud site install to backup
nc_web_dir=/var/www/nextcloud
##### END CONFIG VARIABLES #####
# lock file will be created and used to ensure we don't try to run two
# backup processes at the same time
lockfilename=/var/run/remotebackup.pid
if test -e ${lockfilename}; then
echo "Backup is already running!"
echo "If it isn't, remove ${lockfilename} and try again."
echo "/etc/run_backup: unable to run backup as lock file ${lockfilename} is present" | mail -s "Backup Problem" ${email_receiver}
exit 3
else
echo $$ > ${lockfilename};
fi
remote_server_backup_folder=${remote_backup_storage_root}/${backup_folder}
logfile=${error_report_dir}/rdiff-backup.log
echo "[$(date "+%m%d%Y %T")] Starting backup" >> ${logfile}
# where the database will be backed up to. We don't store the date as we will
# then back up the whole nextcloud data directory using rdiff-backup onto the
# backup server which will allow us to step back in time if required
db_backup_file=${nc_data_dir}/nextcloud-db.bak
cd ${nc_web_dir}
echo "[$(date "+%m%d%Y %T")] Putting nextcloud in maintenance mode" >> ${logfile}
# turn on nextcloud maintenance mode
sudo -u www-data php occ maintenance:mode --on
echo "[$(date "+%m%d%Y %T")] Making nextcloud database dump on the server" >> ${logfile}
mysqldump -u root --single-transaction nextcloud > "${db_backup_file}"
echo "[$(date "+%m%d%Y %T")] Completed nextcloud database dump" >> ${logfile}
#nc -z ${backup_server} 22 > /dev/null
ssh -q root@${backup_server} exit
backup_machine_accessible=$?
echo "in here 0"
if [ "$backup_machine_accessible" -eq "0" ] ;
then
echo "in here 1"
backup_location="root@${backup_server}::${remote_server_backup_folder}"
echo "[$(date "+%m%d%Y %T")] Backing up to ${backup_location}" >> ${logfile}
else
# use local storage (flash drive)
echo "in here 2"
# check the USB drive is actually accesible, then make sure the
# backup directory on the drive actually exists
if test -e ${local_backup_storage_root}; then
echo "in here 3"
backup_location="${local_backup_storage_root}/${backup_folder}"
mkdir -p ${backup_location}
echo "[$(date "+%m%d%Y %T")] ${backup_server} was not accessible, backing up to ${backup_location}" >> ${logfile}
else
echo "in here 4"
echo [$(date "+%m%d%Y %T")] ${backup_server} was not accessible, and ${backup_location} was not available" >> ${logfile}
echo "${backup_server} was not accessible, and ${backup_location} was not also accessible" | mail -s "Backup Problem" ${email_receiver}
if test -e ${lockfilename}; then
rm ${lockfilename}
fi
exit 4
fi
fi
echo "out here"
# make the directory for storing errors reported during the backup process
mkdir -p ${error_report_dir}
# backup nextcloud instance
echo "[$(date "+%m%d%Y %T")] Starting nextcloud web backup" >> ${logfile}
rdiff-backup ${nc_web_dir} ${backup_location}/nextcloud/ 2> ${error_report_dir}/rdiff-backup-nextcloud-web_$(hostname).txt
# backup nextcloud user data
echo "[$(date "+%m%d%Y %T")] Starting nextcloud data backup" >> ${logfile}
rdiff-backup \
--ssh-no-compression \
--exclude "${nc_data_dir}/data/.opcache" \
--exclude "${nc_data_dir}/data/access.log" \
--exclude "${nc_data_dir}/data/error.log" \
--exclude "${nc_data_dir}/data/nextcloud.log" \
--exclude "${nc_data_dir}/data/access.log" \
${nc_data_dir} ${backup_location}/nextcloud-data/ 2> ${error_report_dir}/nextcloud-data-and-db_$(hostname).txt
# turn off nextcloud maintenance mode
cd ${nc_web_dir};
echo "[$(date "+%m%d%Y %T")] Turning off nextcloud maintnenance mode" >> ${logfile}
sudo -u www-data php occ maintenance:mode --off
# backup /etc which includes letsencrypt, fstab
echo "[$(date "+%m%d%Y %T")] /etc backup" >> ${logfile}
rdiff-backup --ssh-no-compression /etc/ ${backup_location}/etc_$(hostname)/ 2> ${error_report_dir}/rdiff-backup-etc_$(hostname).txt
# backup root
echo "[$(date "+%m%d%Y %T")] /root backup" >> ${logfile}
rdiff-backup --ssh-no-compression /root/ ${backup_location}/root_$(hostname)/ 2> ${error_report_dir}/rdiff-backup-root_$(hostname).txt
# backup home directories
echo "[$(date "+%m%d%Y %T")] /home backup" >> ${logfile}
rdiff-backup --ssh-no-compression /home/ ${backup_location}/home_$(hostname) 2> ${error_report_dir}/rdiff-backup-home_$(hostname).txt
echo "/etc/run_backup: backup of $(hostname) appears to have run successfully, or at least reached the end of the script without errors" | mail -s "Backup Report" ${email_receiver}
# Remove lock file and end script
#
if test -e ${lockfilename}; then
rm ${lockfilename}
else
echo "[$(date "+%m%d%Y %T")] Could not remove lock file ${lockfilename}!" >> ${logfile}
echo "/etc/run_backup was unable to remove lock file ${lockfilename} at end of the script as it did not exist" | mail -s "Backup Problem" ${email_receiver}
fi
运行时得到的输出如下:
# /etc/run_backup
+ backup_server=<redacted>
+ backup_folder=backups
+ remote_backup_storage_root=/mnt/reos-storage-2
+ local_backup_storage_root=/media/usbflash0
+ error_report_dir=/home/pi/rdiff-backup-errors
+ email_receiver=<redacted>
+ nc_data_dir=/opt/nextcloud-data
+ nc_web_dir=/var/www/nextcloud
+ lockfilename=/var/run/remotebackup.pid
+ test -e /var/run/remotebackup.pid
+ echo 13057
+ remote_server_backup_folder=/mnt/reos-storage-2/backups
+ logfile=/home/pi/rdiff-backup-errors/rdiff-backup.log
+ date +%m%d%Y %T
+ echo [09162019 11:30:06] Starting backup
+ db_backup_file=/opt/nextcloud-data/nextcloud-db.bak
+ cd /var/www/nextcloud
+ date +%m%d%Y %T
+ echo [09162019 11:30:06] Putting nextcloud in maintenance mode
+ sudo -u www-data php occ maintenance:mode --on
Maintenance mode already enabled
+ date +%m%d%Y %T
+ echo [09162019 11:30:07] Making nextcloud database dump on the server
+ mysqldump -u root --single-transaction nextcloud
+ date +%m%d%Y %T
+ echo [09162019 11:30:12] Completed nextcloud database dump
+ ssh -q root@ed-mh-pi01.reoptimizesystems.com exit
+ backup_machine_accessible=0
+ echo in here 0
in here 0
/etc/run_backup: 173: /etc/run_backup: Syntax error: Unterminated quoted string
有人可以解释该错误吗?在我看来,这与if语句if [ "$backup_machine_accessible" -eq "0" ] ;
有关,但是我看不出有任何问题。