│ │192.168.1.1 ┏┷┓ ┃ ┃ ┃ ┃DB example1 ┃ ┃ ┗┯┛ │192.168.2.1 │ │192.168.2.2 ┏┷┓ ┃ ┃ ┃ ┃NAS example2 ┃ ┃ ┗┯┛ │
[postgres@example1 postgres]$ mkdir /home/postgres/backup [postgres@example1 postgres]$ vi /home/postgres/backup.sh # DB Backup Script # dbsave.sh DBname days(passed for delete) echo "DB Maintenance Starts at `date`" if [ $# != 0 ] then dbname=$1 export PATH="$PATH":/usr/local/pgsql/bin export LD_LIBRARY_PATH="$LD_LIBRARY_PATH":/usr/local/pgsql/lib fdir="/home/postgres/backup"
fname="$fdir/$dbname.`date '+%y%m%d%H%M%S'`.dump" echo "DB Vacuum $dbname" vacuumdb $dbname echo "DB Backup $dbname into $fname" pg_dump -Fc -b -o $dbname > $fname if [ -z $2 ] then days=7 else days=$2 fi echo "DB Old Backup files($days days passed) delete" find $fdir -mtime +$days -name ${dbname}* -print -exec rm -f {} \; fi echo "DB Maintenance Ends at `date`" [postgres@example1 postgres]$ chmod 770 /home/postgres/backup.sh
[postgres@example1 postgres]$ crontab -e MAILTO="root" 5 5 * * * /home/postgres/backup.sh dbname 5
[postgres@example2 postgres]$ mkdir /home/postgres/backupall
[postgres@example2 postgres]$ vi /home/postgres/login.sh open 192.168.2.1 user postgres xxxxxxxx idle 600 prompt lcd /home/postgres/backupall cd /home/postgres/backup mls /home/postgres/backup /home/postgres/remote.list bye [postgres@example2 postgres]$ vi remote_1.sh #!/bin/sh login=/home/postgres/login.sh cd /usr/bin ftp -n < ${login}
[postgres@example2 postgres]$ vi remote_2.sh #!/bin/sh msgfile=/home/postgres/remote.list ipaddr=192.168.2.1 dbuser=postgres dbpass=xxxxxxxx nasdir=/home/postgres/backupall dbdir=/home/postgres/backup offset=22 login=/home/postgres/daily_login.sh filename=dbname.`date +%y%m%d` todump=${dbdir}/$filename while read line do if [ $(echo $line | grep ${todump}) ];then dumpfile=${line:${offset}} printf "open ${ipaddr}\nuser ${dbuser} ${dbpass}\nidle 600\nprompt\nlcd ${nasdir}\ncd ${dbdir}\nbinary\nget ${dumpfile}\n" > ${login} /bin/chmod 775 ${login} break fi done < $msgfile [postgres@example2 postgres]$ vi remote_3.sh #!/bin/sh login=/home/postgres/daily_login.sh cd /usr/bin ftp -n < ${login} [postgres@example2 postgres]$ chmod 775 login.sh [postgres@example2 postgres]$ chmod 775 remote_1.sh [postgres@example2 postgres]$ chmod 775 remote_2.sh [postgres@example2 postgres]$ chmod 775 remote_3.sh
[root@example2 root]# vi /etc/crontab SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/ # run-parts 01 * * * * root run-parts /etc/cron.hourly 02 4 * * * root run-parts /etc/cron.daily 22 4 * * 0 root run-parts /etc/cron.weekly 42 4 1 * * root run-parts /etc/cron.monthly # Backup 30 5 * * * postgres /home/postgres/remote_1.sh > /dev/null 2 >&1 40 5 * * * postgres /home/postgres/remote_2.sh > /dev/null 2 >&1 50 5 * * * postgres /home/postgres/remote_3.sh > /dev/null 2 >&1