我有一台安装了 MS-SQLServer 的 Ubuntu Server 16.04 服务器。
我试图在每周星期四 16:00:00 为数据库创建计划备份。
创建备份的脚本是:
#!/bin/bash
echo "Creating backup directory"
fn="backup-"$(date +"%d-%m-%Y")
dir="/data/mssql/$fn"
echo $dir
mkdir $dir
echo "Creating backups"
sqlcmd -H localhost -U SA -P Password -Q "Backup database [DB1] to disk='$dir/DB1.bak'"
sqlcmd -H localhost -U SA -P Password -Q "Backup database [DB2] to disk='$dir/DB2.bak'"
echo "Backups created, creating MD5 checksums"
md5sum $dir/* > $dir/checksum
echo "Checksums created, preparing for upload by compressing folder"
tar -zcvf $fn.tar.gz $dir
我使用 Systemd Timers 来安排备份
过程sqlback.timer
:
[Unit]
Description= Schedule a weekly backup of MS-SQL Server database
[Timer]
OnCalendar=thu 16:00
Persistent=true
Unit=sqlback.service
[Install]
WantedBy=timers.target
和服务文件:
[Unit]
Description= Create weekly backup of MS-SQL Server databases for the DB1 and DB2
[Service]
User=mssql
Group=mssql
Type=simple
ExecStart=/bin/bash /usr/bin/sql_backup.sh
[Install]
WantedBy=multi-user.target
当服务按计划启动时,备份创建成功,接下来是压缩,tar
失败并显示以下消息:
آب 15 12:53:22 main bash[9576]: tar: backup-15-08-2018.tar.gz: Cannot write: Broken pipe
آب 15 12:53:22 main bash[9576]: tar: Error is not recoverable: exiting now
sudo
但是,如果我使用和手动运行脚本su
,一切都正常工作。
答案1
将完整路径输入到fn
变量中。或者添加
cd $dir
还要验证mssql
用户是否有写入备份目录的权限。