-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
28 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,34 @@ | ||
#!/bin/bash | ||
|
||
#In "xxx" place put your data | ||
#In "your_location" place put your data | ||
#Location to place backups. | ||
BACKUP_DIR="/storage/xxx/" | ||
BACKUP_DIR="/storage/your_location/" | ||
|
||
#String to append to the name of the backup files | ||
BACKUP_DATE=`date +%d-%m-%Y-%H%M%S` | ||
|
||
#DataBase settings | ||
DB_NAME="xxx" | ||
DB_HOST="xxx" | ||
DB_PORT="xxx" | ||
DB_USER="postgres" | ||
DB_PASSWORD="xxx" | ||
#DataBase settings keep it secret | ||
DB_NAME="data_base_name" | ||
DB_HOST="data_base_host" | ||
DB_PORT="data_base_port" | ||
DB_USER="data_base_user" | ||
DB_PASSWORD="data_base_password" | ||
|
||
#Numbers of days you want to keep copy of your databases | ||
NUMBER_OF_DAYS=180 | ||
NUMBER_OF_DAYS=30 | ||
echo "Dumping database to ${BACKUP_DIR}${DB_NAME}_${BACKUP_DATE}.sql" | ||
|
||
if [ ! -d "${BACKUP_DIR}" ]; then | ||
mkdir -p "${BACKUP_DIR}" | ||
fi | ||
|
||
PGPASSWORD="${DB_PASSWORD}" pg_dump -h "${DB_HOST}" -p "${DB_PORT}" -U "${DB_USER}" -d "${DB_NAME}" -w --format=custom | xz > "${BACKUP_DIR}${DB_NAME}_${BACKUP_DATE}.xz" | ||
|
||
echo "Dumping database finnish" | ||
|
||
find "${BACKUP_DIR}" -type f -prune -mtime +"${NUMBER_OF_DAYS}" -exec rm -f {} \; | ||
# Attempt to create the backup | ||
if PGPASSWORD="${DB_PASSWORD}" pg_dump -h "${DB_HOST}" -p "${DB_PORT}" -U "${DB_USER}" -d "${DB_NAME}" -w --format=custom | xz > "${BACKUP_DIR}${DB_NAME}_${BACKUP_DATE}.xz"; then | ||
echo "Dumping database finished successfully" | ||
|
||
# Delete old backups, but only if a current backup exists | ||
find "${BACKUP_DIR}" -type f -prune -mtime +"${NUMBER_OF_DAYS}" -exec rm -f {} \; | ||
echo "Old backups deleted" | ||
else | ||
echo "Error: Dumping database failed" | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters