Browse Source

postgresql backup

bach 3 năm trước cách đây
mục cha
commit
5de5b48682
2 tập tin đã thay đổi với 81 bổ sung26 xóa
  1. 27 26
      assets/pgsqlbackup.sh
  2. 54 0
      bin/postgresqlbackup.sh

+ 27 - 26
assets/pgsqlbackup.sh

@@ -4,25 +4,21 @@
 # Parent backup directory
 backup_parent_dir="/var/backups/postgresql"
 
-# MySQL settings
-postgresql_user="root"
-postgresql_password="ROOTPASSWD"
-
-# Read MySQL password from stdin if empty
-# if [ -z "${mysql_password}" ]; then
-#   echo -n "Enter MySQL ${mysql_user} password: "
-#   read -s mysql_password
-#   echo
-# fi
+# PostgreSQL settings
+pg_host="HOST"
+pg_port="PORT"
+pg_user="USER"
+pg_password="PASSWD"
+
 
 # Check MySQL password
-echo exit | mysql --user=${mysql_user} --password=${mysql_password} -B 2>/dev/null
-if [ "$?" -gt 0 ]; then
-  echo "MySQL ${mysql_user} password incorrect"
-  exit 1
-else
-  echo "MySQL ${mysql_user} password correct."
-fi
+# echo exit | mysql --user=${mysql_user} --password=${mysql_password} -B 2>/dev/null
+# if [ "$?" -gt 0 ]; then
+#   echo "MySQL ${mysql_user} password incorrect"
+#   exit 1
+# else
+#   echo "MySQL ${mysql_user} password correct."
+# fi
 
 # Create backup directory and set permissions
 backup_date=`date +%Y_%m_%d_%H_%M`
@@ -32,19 +28,24 @@ mkdir -p "${backup_dir}"
 chmod 644 "${backup_dir}"
 
 # Get postgresql databases
-pgsql_databases=`echo 'show databases' | psql -h localhost -U postgres `
+pgsql_databases=`psql "host=$pg_host port=$pg_port user=$pg_user password=$pg_password" -At -c "select datname from pg_database where not datistemplate and datallowconn;"`
+
 
 # Backup and compress each database
-for database in $mysql_databases
+for database in $pgsql_databases
 do
-  if [ "${database}" == "information_schema" ] || [ "${database}" == "performance_schema" ]; then
-        additional_mysqldump_params="--skip-lock-tables --compact --no-autocommit "
-  else
-        additional_mysqldump_params=""
-  fi
   echo "Creating backup of \"${database}\" database"
-  mysqldump ${additional_mysqldump_params} --user=${mysql_user} --password=${mysql_password} ${database} | gzip > "${backup_dir}/${database}.sql.gz"
-  chmod 644 "${backup_dir}/${database}.sql.gz"
+  # mysqldump ${additional_mysqldump_params} --user=${mysql_user} --password=${mysql_password} ${database} | gzip > "${backup_dir}/${database}.sql.gz"
+  # chmod 644 "${backup_dir}/${database}.sql.gz"
+	set -o pipefail
+	# if ! pg_dump -Fp -h "$pg_host" -U "$pg_user" "$database" | gzip > $backup_dir"/$database".sql.gz.in_progress; then
+  # if ! pg_dump -Fp "host=$pg_host port=$pg_port user=$pg_user password=$pg_password" "$database" | gzip > $backup_dir"/$database".sql.gz.in_progress; then
+  if ! pg_dump -Fp --dbname="postgresql://$pg_user:$pg_password@$pg_host:$pg_port/$database" | gzip > $backup_dir"/$database".sql.gz.in_progress; then
+		echo "[!!ERROR!!] Failed to produce plain backup database $database" 1>&2
+	else
+		mv $backup_dir"/$database".sql.gz.in_progress $backup_dir"/$database".sql.gz
+	fi
+	set +o pipefail
 done
 
 # compress the folder

+ 54 - 0
bin/postgresqlbackup.sh

@@ -0,0 +1,54 @@
+#!/bin/sh
+
+echo -e '\033[35m
+  ___        _                ___  ___  _      ___          _
+ | _ \___ __| |_ __ _ _ _ ___/ __|/ _ \| |    | _ ) __ _ __| |___  _ _ __
+ |  _/ _ (_-<  _/ _` | '_/ -_)__ \ (_) | |__  | _ \/ _` / _| / / || | '_ \
+ |_| \___/__/\__\__, |_| \___|___/\__\_\____| |___/\__,_\__|_\_\\_,_| .__/
+                |___/                                               |_|
+\033[0m'
+
+if [ "$EUID" -ne 0 ]; then
+  echo "Please run as root"
+  exit
+fi
+
+# get the current position
+_cwd="$(pwd)"
+# check for assets forlder
+_assets="$_cwd/assets"
+if [ ! -d "$_assets" ]; then
+  _assets="$_cwd/../assets"
+  if [ ! -d "$_assets" ]; then
+    echo "!! can't find assets directory !!"
+    exit
+  fi
+fi
+
+# adding the script
+cp "$_assets"/pgsqlbackup.sh /usr/local/bin/
+chmod +x /usr/local/bin/pgsqlbackup.sh
+
+# configure
+echo -n "Please provide the postgresql host : "
+read _pg_host
+sed -i "s/HOST/$_pg_host/g" /usr/local/bin/pgsqlbackup.sh
+
+echo -n "Please provide the postgresql port : "
+read _pg_port
+sed -i "s/PORT/$_pg_port/g" /usr/local/bin/pgsqlbackup.sh
+
+echo -n "Please provide the postgresql user : "
+read _pg_user
+sed -i "s/USER/$_pg_user/g" /usr/local/bin/pgsqlbackup.sh
+
+echo -n "Please provide the postgresql passwd : "
+read _pg_passwd
+sed -i "s/PASSWD/$_pg_passwd/g" /usr/local/bin/pgsqlbackup.sh
+
+# creating crontab
+touch /var/spool/cron/crontabs/root
+crontab -l > /tmp/mycron
+echo "30 2 */2 * * /usr/local/bin/pgsqlbackup.sh" >> /tmp/mycron
+crontab /tmp/mycron
+rm /tmp/mycron