- 设置环境变量
编辑 /etc/profile 文件,在文件末尾添加以下行:
export KINGBASE_PASSWORD='password'
使更改生效:
source /etc/profile
打印环境变量
echo $KINGBASE_PASSWORD
- 备份脚本 auto_backup.sh
#!/bin/bash
# 从脚本参数中读取数据库名称
if [ $# -eq 0 ]; then
echo "Usage: $0 <database>"
exit 1
fi
database=$1
# 检查数据库名称是否有效
if [ -z "$database" ]; then
echo "Error: Database name cannot be empty."
exit 1
fi
# 动态生成备份文件名
timestamp=$(date +"%Y%m%d%H%M%S")
backup_path="/opt/Kingbase/ES/V8/backup"
backup_file="${backup_path}/${database}_$timestamp.sql"
echo "Backup file: $backup_file"
# 检查备份路径是否存在并可写
if [ ! -d "$backup_path" ] || [ ! -w "$backup_path" ]; then
echo "Error: Backup path does not exist or is not writable."
exit 1
fi
# 执行备份命令
echo "Executing backup command: /opt/Kingbase/ES/V8/Server/bin/sys_dump -h 127.0.0.1 -p 54321 -U system -f $backup_file $database"
# 使用 && 来确保命令执行成功
/opt/Kingbase/ES/V8/Server/bin/sys_dump -h 127.0.0.1 -p 54321 -U system -f "$backup_file" "$database"
if [ $? -ne 0 ]; then
echo "Error executing backup command."
exit 1
else
echo "Backup completed successfully."
fi
- 执行脚本(test为数据库名)
sh auto_backup.sh test