Created
October 29, 2016 05:16
-
-
Save bspkrs/452b4037f6685916062f4338e990f52b to your computer and use it in GitHub Desktop.
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
#!/bin/bash | |
# Runs pg_dump and runs a file rotation script to manage the files | |
# Author bspkrs (bspkrs@gmail.com) | |
echo Running Backup Script on $(date +"%C%y%m%d %T")... >> /var/lib/pgsql/backup.log | |
stagefolder=/var/lib/pgsql/newdump/ | |
# Run the backup | |
/usr/pgsql-9.3/bin/pg_dump -Fc <databasename> > ${stagefolder}/db_$(date +%C%y%m%d).dmp | |
# Push the new dump file out to where it needs to go | |
/var/lib/pgsql/rotate_backups.sh *.dmp ${stagefolder} /var/lib/pgsql/backups/ | |
# Delete the dump file from the stage folder | |
rm -f ${stagefolder}/*.dmp |
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
#!/bin/bash | |
# Julius Zaromskis, bspkrs | |
# Backup rotation (adapted from http://nicaw.wordpress.com/2013/04/18/bash-backup-rotation-script/) | |
# Storage folder where to move backup files | |
# Must contain backup.monthly backup.weekly backup.daily folders | |
target=$3 | |
if [ ! -d ${target} ] ; then | |
mkdir ${target} | |
fi | |
if [ ! -d ${target}/backup.monthly ]; then | |
mkdir ${target}/backup.monthly | |
fi | |
if [ ! -d ${target}/backup.weekly ]; then | |
mkdir ${target}/backup.weekly | |
fi | |
if [ ! -d ${target}/backup.daily ]; then | |
mkdir ${target}/backup.daily | |
fi | |
# Source folder where files are dumped | |
source=$2 | |
# Source filename (can be wildcarded) | |
filename=$1 | |
# Get current month and week day number | |
month_day=`date +"%d"` | |
week_day=`date +"%u"` | |
# Optional check if source files exist. Email if failed. | |
if [ ! -f ${source}/${filename} ]; then | |
ls -l ${source}/ | mail bspkrs@gmail.com -s "[backup script] Daily backup failed! Please check for missing files." | |
fi | |
# It is logical to run this script daily. We take files from source folder and move them to | |
# appropriate destination folder | |
# On first month day do | |
if [ "$month_day" -eq 1 ]; then | |
destination=${target}/backup.monthly/ | |
else | |
# On saturdays do | |
if [ "$week_day" -eq 6 ]; then | |
destination=${target}/backup.weekly/ | |
else | |
# On any regular day do | |
destination=${target}/backup.daily/ | |
fi | |
fi | |
# Move the files | |
cp ${source}/$filename ${destination} | |
# daily - keep for 30 days | |
find ${target}/backup.daily/ -maxdepth 1 -mtime +30 -type d -exec rm -rv {} \; | |
# weekly - keep for 90 days | |
find $target/backup.weekly/ -maxdepth 1 -mtime +90 -type d -exec rm -rv {} \; | |
# monthly - keep for 365 days | |
find ${target}/backup.monthly/ -maxdepth 1 -mtime +365 -type d -exec rm -rv {} \; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment