Added PostgreSQL and SQLite backup scripts.
This commit is contained in:
parent
a9da8dd5b9
commit
3c968f1beb
2 changed files with 199 additions and 0 deletions
112
modules/rhizomatica_base_system/files/bin/pg_backup_rotated.sh
Executable file
112
modules/rhizomatica_base_system/files/bin/pg_backup_rotated.sh
Executable file
|
@ -0,0 +1,112 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Username to connect to database as. Will default to "postgres" if none specified.
|
||||||
|
USERNAME=postgres
|
||||||
|
|
||||||
|
# This dir will be created if it doesn't exist. This must be writable by the user the script is
|
||||||
|
# running as.
|
||||||
|
BACKUP_DIR=/var/rhizo_backups/postgresql/
|
||||||
|
|
||||||
|
# Will produce a custom-format backup if set to "yes"
|
||||||
|
ENABLE_CUSTOM_BACKUPS=yes
|
||||||
|
|
||||||
|
# Will produce a gzipped plain-format backup if set to "yes"
|
||||||
|
ENABLE_PLAIN_BACKUPS=yes
|
||||||
|
|
||||||
|
# Which day to take the weekly backup from (1-7 = Monday-Sunday)
|
||||||
|
DAY_OF_WEEK_TO_KEEP=6
|
||||||
|
|
||||||
|
# Number of days to keep daily backups
|
||||||
|
DAYS_TO_KEEP=7
|
||||||
|
|
||||||
|
# How many weeks to keep weekly backups
|
||||||
|
WEEKS_TO_KEEP=5
|
||||||
|
|
||||||
|
|
||||||
|
function perform_backups()
|
||||||
|
{
|
||||||
|
SUFFIX=$1
|
||||||
|
FINAL_BACKUP_DIR=$BACKUP_DIR"`date +\%Y-\%m-\%d`$SUFFIX/"
|
||||||
|
|
||||||
|
echo "Making backup directory in $FINAL_BACKUP_DIR"
|
||||||
|
|
||||||
|
if ! mkdir -p $FINAL_BACKUP_DIR; then
|
||||||
|
echo "Cannot create backup directory in $FINAL_BACKUP_DIR. Go and fix it!" 1>&2
|
||||||
|
exit 1;
|
||||||
|
fi;
|
||||||
|
|
||||||
|
|
||||||
|
###########################
|
||||||
|
###### FULL BACKUPS #######
|
||||||
|
###########################
|
||||||
|
|
||||||
|
FULL_BACKUP_QUERY="select datname from pg_database where not datistemplate and datallowconn order by datname;"
|
||||||
|
|
||||||
|
echo -e "\n\nPerforming full backups"
|
||||||
|
echo -e "--------------------------------------------\n"
|
||||||
|
|
||||||
|
for DATABASE in `psql -U "$USERNAME" -At -c "$FULL_BACKUP_QUERY" postgres`
|
||||||
|
do
|
||||||
|
if [ $ENABLE_PLAIN_BACKUPS = "yes" ]
|
||||||
|
then
|
||||||
|
echo "Plain backup of $DATABASE"
|
||||||
|
|
||||||
|
if ! pg_dump -Fp -U "$USERNAME" "$DATABASE" | gzip > $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress; then
|
||||||
|
echo "[!!ERROR!!] Failed to produce plain backup database $DATABASE" 1>&2
|
||||||
|
else
|
||||||
|
mv $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress $FINAL_BACKUP_DIR"$DATABASE".sql.gz
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ $ENABLE_CUSTOM_BACKUPS = "yes" ]
|
||||||
|
then
|
||||||
|
echo "Custom backup of $DATABASE"
|
||||||
|
|
||||||
|
if ! pg_dump -Fc -U "$USERNAME" "$DATABASE" -f $FINAL_BACKUP_DIR"$DATABASE".custom.in_progress; then
|
||||||
|
echo "[!!ERROR!!] Failed to produce custom backup database $DATABASE"
|
||||||
|
else
|
||||||
|
mv $FINAL_BACKUP_DIR"$DATABASE".custom.in_progress $FINAL_BACKUP_DIR"$DATABASE".custom
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
done
|
||||||
|
|
||||||
|
echo -e "\nAll database backups complete!"
|
||||||
|
}
|
||||||
|
|
||||||
|
# MONTHLY BACKUPS
|
||||||
|
|
||||||
|
DAY_OF_MONTH=`date +%d`
|
||||||
|
|
||||||
|
if [ $DAY_OF_MONTH -eq 1 ];
|
||||||
|
then
|
||||||
|
# Delete all expired monthly directories
|
||||||
|
find $BACKUP_DIR -maxdepth 1 -name "*-monthly" -exec rm -rf '{}' ';'
|
||||||
|
|
||||||
|
perform_backups "-monthly"
|
||||||
|
|
||||||
|
exit 0;
|
||||||
|
fi
|
||||||
|
|
||||||
|
# WEEKLY BACKUPS
|
||||||
|
|
||||||
|
DAY_OF_WEEK=`date +%u` #1-7 (Monday-Sunday)
|
||||||
|
EXPIRED_DAYS=`expr $((($WEEKS_TO_KEEP * 7) + 1))`
|
||||||
|
|
||||||
|
if [ $DAY_OF_WEEK = $DAY_OF_WEEK_TO_KEEP ];
|
||||||
|
then
|
||||||
|
# Delete all expired weekly directories
|
||||||
|
find $BACKUP_DIR -maxdepth 1 -mtime +$EXPIRED_DAYS -name "*-weekly" -exec rm -rf '{}' ';'
|
||||||
|
|
||||||
|
perform_backups "-weekly"
|
||||||
|
|
||||||
|
exit 0;
|
||||||
|
fi
|
||||||
|
|
||||||
|
# DAILY BACKUPS
|
||||||
|
|
||||||
|
# Delete daily backups 7 days old or more
|
||||||
|
find $BACKUP_DIR -maxdepth 1 -mtime +$DAYS_TO_KEEP -name "*-daily" -exec rm -rf '{}' ';'
|
||||||
|
|
||||||
|
perform_backups "-daily"
|
||||||
|
|
87
modules/rhizomatica_base_system/files/bin/sqlite_backup_rotated.sh
Executable file
87
modules/rhizomatica_base_system/files/bin/sqlite_backup_rotated.sh
Executable file
|
@ -0,0 +1,87 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
#Where the SQLite file is located
|
||||||
|
DATABASE_FILE=/var/lib/osmocom/hlr.sqlite3
|
||||||
|
|
||||||
|
#Name of the database backup
|
||||||
|
DATABASE_NAME=hlr
|
||||||
|
|
||||||
|
# This dir will be created if it doesn't exist.
|
||||||
|
BACKUP_DIR=/var/rhizo_backups/sqlite/
|
||||||
|
|
||||||
|
# Which day to take the weekly backup from (1-7 = Monday-Sunday)
|
||||||
|
DAY_OF_WEEK_TO_KEEP=6
|
||||||
|
|
||||||
|
# Number of days to keep daily backups
|
||||||
|
DAYS_TO_KEEP=7
|
||||||
|
|
||||||
|
# How many weeks to keep weekly backups
|
||||||
|
WEEKS_TO_KEEP=5
|
||||||
|
|
||||||
|
function perform_backups()
|
||||||
|
{
|
||||||
|
SUFFIX=$1
|
||||||
|
FINAL_BACKUP_DIR=$BACKUP_DIR"`date +\%Y-\%m-\%d`$SUFFIX/"
|
||||||
|
|
||||||
|
echo "Making backup directory in $FINAL_BACKUP_DIR"
|
||||||
|
|
||||||
|
if ! mkdir -p $FINAL_BACKUP_DIR; then
|
||||||
|
echo "Cannot create backup directory in $FINAL_BACKUP_DIR. Go and fix it!" 1>&2
|
||||||
|
exit 1;
|
||||||
|
fi;
|
||||||
|
|
||||||
|
|
||||||
|
###########################
|
||||||
|
###### FULL BACKUPS #######
|
||||||
|
###########################
|
||||||
|
|
||||||
|
echo -e "\n\nPerforming full backup"
|
||||||
|
echo -e "--------------------------------------------\n"
|
||||||
|
|
||||||
|
echo "Plain backup of $DATABASE_FILE"
|
||||||
|
|
||||||
|
if ! sqlite3 "$DATABASE_FILE" ".dump" | gzip > $FINAL_BACKUP_DIR"$DATABASE_NAME".sql.gz.in_progress; then
|
||||||
|
echo "[!!ERROR!!] Failed to produce plain backup database $DATABASE_FILE" 1>&2
|
||||||
|
else
|
||||||
|
mv $FINAL_BACKUP_DIR"$DATABASE_NAME".sql.gz.in_progress $FINAL_BACKUP_DIR"$DATABASE_NAME".sql.gz
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo -e "\nDatabase backup complete!"
|
||||||
|
}
|
||||||
|
|
||||||
|
# MONTHLY BACKUPS
|
||||||
|
|
||||||
|
DAY_OF_MONTH=`date +%d`
|
||||||
|
|
||||||
|
if [ $DAY_OF_MONTH -eq 1 ];
|
||||||
|
then
|
||||||
|
# Delete all expired monthly directories
|
||||||
|
find $BACKUP_DIR -maxdepth 1 -name "*-monthly" -exec rm -rf '{}' ';'
|
||||||
|
|
||||||
|
perform_backups "-monthly"
|
||||||
|
|
||||||
|
exit 0;
|
||||||
|
fi
|
||||||
|
|
||||||
|
# WEEKLY BACKUPS
|
||||||
|
|
||||||
|
DAY_OF_WEEK=`date +%u` #1-7 (Monday-Sunday)
|
||||||
|
EXPIRED_DAYS=`expr $((($WEEKS_TO_KEEP * 7) + 1))`
|
||||||
|
|
||||||
|
if [ $DAY_OF_WEEK = $DAY_OF_WEEK_TO_KEEP ];
|
||||||
|
then
|
||||||
|
# Delete all expired weekly directories
|
||||||
|
find $BACKUP_DIR -maxdepth 1 -mtime +$EXPIRED_DAYS -name "*-weekly" -exec rm -rf '{}' ';'
|
||||||
|
|
||||||
|
perform_backups "-weekly"
|
||||||
|
|
||||||
|
exit 0;
|
||||||
|
fi
|
||||||
|
|
||||||
|
# DAILY BACKUPS
|
||||||
|
|
||||||
|
# Delete daily backups 7 days old or more
|
||||||
|
find $BACKUP_DIR -maxdepth 1 -mtime +$DAYS_TO_KEEP -name "*-daily" -exec rm -rf '{}' ';'
|
||||||
|
|
||||||
|
perform_backups "-daily"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue