adding postgresql backup script
This commit is contained in:
parent
744597495d
commit
e017320e8c
10
midgard/backup/install
Executable file
10
midgard/backup/install
Executable file
@ -0,0 +1,10 @@
|
||||
#!/bin/sh
|
||||
|
||||
set -e
|
||||
|
||||
BASEDIR=$(dirname "$0")
|
||||
|
||||
install -o root -g root -m 755 "$BASEDIR/mkpsqlbackup" /usr/local/bin/mkpsqlbackup
|
||||
install -o root -g root -m 644 "$BASEDIR/mkpsqlbackup.conf" /etc/mkpsqlbackup.conf
|
||||
install -o root -g root -m 644 "$BASEDIR/mkpsqlbackup.service" /etc/systemd/system/mkpsqlbackup.service
|
||||
install -o root -g root -m 644 "$BASEDIR/mkpsqlbackup.timer" /etc/systemd/system/mkpsqlbackup.timer
|
35
midgard/backup/mkpsqlbackup
Normal file
35
midgard/backup/mkpsqlbackup
Normal file
@ -0,0 +1,35 @@
|
||||
#!/bin/sh
|
||||
|
||||
set -e
|
||||
|
||||
# Default configuration
|
||||
DATABASES=''
|
||||
BACKUP_DIR=/data/backup/postgresql
|
||||
RETENTION_DAYS=7
|
||||
|
||||
if ! source /etc/mkpsqlbackup.conf ; then
|
||||
echo '[MKPSQLBACKUP] : Unable to load configuration file' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ "$(id -un)" != 'postgres' ] ; then
|
||||
echo '[MKPSQLBACKUP] : This script must be run as postgres user' >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
FINAL_BACKUP_DIR="$BACKUP_DIR/-$(date '+%Y-%m-%d')"
|
||||
echo "[MKPSQLBACKUP] : Creating backup directory $FINAL_BACKUP_DIR"
|
||||
if ! mkdir -p "$FINAL_BACKUP_DIR" ; then
|
||||
echo "[MKPSQLBACKUP] : Unable to create backup directory $FINAL_BACKUP_DIR"
|
||||
exit 3
|
||||
fi
|
||||
|
||||
for db in DATABASES ; do
|
||||
echo "[MKPSQLBACKUP] : Backup of $db"
|
||||
if ! pg_dump -Fc "$db" -f "$FINAL_BACKUP_DIR/$db" ; then
|
||||
echo "[MKPSQLBACKUP] : Unable to backup database $db" >&2
|
||||
fi
|
||||
done
|
||||
|
||||
echo '[MKPSQLBACKUP] : Purging old backups'
|
||||
find "$BACKUP_DIR" -maxdepth 1 mindepth 1 -mtime +"$RETENTION_DAYS" -exec rm -rf '{}' ';'
|
3
midgard/backup/mkpsqlbackup.conf
Normal file
3
midgard/backup/mkpsqlbackup.conf
Normal file
@ -0,0 +1,3 @@
|
||||
DATABASES=''
|
||||
BACKUP_DIR=/data/backup/postgresql
|
||||
RETENTION_DAYS=7
|
5
midgard/backup/mkpsqlbackup.service
Normal file
5
midgard/backup/mkpsqlbackup.service
Normal file
@ -0,0 +1,5 @@
|
||||
[Unit]
|
||||
Description=Performing PostgreSQL database backups
|
||||
|
||||
[Service]
|
||||
ExecStart=/usr/local/bin/mkpsqlbackup
|
10
midgard/backup/mkpsqlbackup.timer
Normal file
10
midgard/backup/mkpsqlbackup.timer
Normal file
@ -0,0 +1,10 @@
|
||||
[Unit]
|
||||
Description=Performing PostgreSQL database backups every days at 3:00am
|
||||
|
||||
[Timer]
|
||||
# Run every days at 3:00am
|
||||
OnCalendar=*-*-* 3:00:00
|
||||
Persistent=true
|
||||
|
||||
[Install]
|
||||
WantedBy=timers.target
|
Loading…
x
Reference in New Issue
Block a user