1、贴数据库的备份shell脚本,命名为back.sh,放在/opt目录下
#!/bin/bash
HOME="postgresql安装路径下的lib,如/data/services/postgresql9/lib"
cur_time=`date +%Y-%m-%d`
if [ "$LD_LIBRARY_PATH" != "$HOME" ]; then
echo "no LD_LIBRARY_PATH"
export LD_LIBRARY_PATH=$HOME
echo "set LD_LIBRARY_PATH"
fi
if [ "$PGPASSWORD" != "xxxx" ]; then
echo "$(date): no password"
export PGPASSWORD="xxxx"
#echo "$PGPASSWORD"
/data/services/postgresql9/bin/pg_dump -h ip -U user -d database -p port -f /home/databasebak/database_$cur_time.bak
echo "$(date):database backup success"
else
echo "$(date): has password"
/data/services/postgresql9/bin/pg_dump -h ip -U user -d database -p port -f /home/databasebak/database_$cur_time.bak
echo "$(date):database backup success"
fi
2、对linux设置定时任务,定时的执行这个备份的脚本
inux设置定时任务的关键字是:crontab
查看现在已经有的定时任务的命令是 crontab -l
新建定时任务的命令是:crontab -e 类似于vi的编辑
* 0 * * * sh /opt/back.sh