Postgres备份全库:

#!/bin/bash

list=$(psql -tqc "SELECT datname FROM pg_database WHERE datistemplate = false")

for d in $list; do  
  echo "backing up $d"
  pg_dump --no-owner --clean $d > ./pg_$d.sql
done  

使用:

PGHOST=ip PGUSER=postgres PGPASSWORD=pass PGDATABASE=postgres ./pg_dump_all  

Mysql备份全库:

#!/bin/bash

# Grab all user databases while skipping any system ones
list=$(mysql -h $MYSQL_HOST -u $MYSQL_USER -p$MYSQL_PASSWORD --skip-column-names -e "SHOW DATABASES;" | grep -v 'mysql\|_schema')

for d in $list; do  
  echo "backing up $d"
  mysqldump -h $MYSQL_HOST -u $MYSQL_USER -p$MYSQL_PASSWORD --quote-names $d > ./mysql_$d.sql
done  

使用:

$ MYSQL_HOST=ip MYSQL_USER=root MYSQL_PASSWORD=pass ./mysql_dump_all
comments powered by Disqus