-
-
Save hlopez/9115991 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/zsh | |
DB_NAME=$1 | |
SSH_USER='' | |
SSH_SERVER='' | |
BACKUPS_DIR='/backup/full_backups' | |
DATE=`date +"%Y-%m-%d"` | |
DIR_NAME="${DATE}_daily" | |
FULL_BACKUP_PATH="$BACKUPS_DIR/$DIR_NAME" | |
BACKUP_FILES=( "DUMP_SAPEC_$DATE.sql.gz" "SHARED_SAPEC_$DATE.tar.gz" ) | |
LOCAL_BACKUPS_PATH="$HOME/code/backups/sapec" | |
if [[ $# < 1 ]]; then | |
echo "Usage: drop_backup_sapec.sh DB_NAME" | |
exit 1 | |
fi | |
function drop_database() | |
{ | |
local db_name=$1 | |
if ! (dropdb "${db_name}_development" &> /dev/null); then | |
echo "Error trying to drop $db_name database" | |
exit 1 | |
fi | |
} | |
# FIXME: Customize this function | |
function uncompress_and_restore_file() | |
{ | |
local backup_file=$1 | |
local uncompressed_file="${backup_file%.*}" | |
if [[ "$backup_file" =~ ".tar.gz$" ]]; then | |
tar xfz $backup_file | |
rm -rf "$HOME/code/sapec/public/system" | |
mv var/WebApps/sapec/shared/public/system "$HOME/code/sapec/public" | |
elif [[ "$backup_file" =~ "\.sql\.gz$" ]];then | |
gunzip $backup_file | |
drop_database $DB_NAME | |
createdb "${DB_NAME}_development" | |
psql "${DB_NAME}_development" &> /dev/null < $uncompressed_file | |
fi | |
} | |
function copy_remote_file() | |
{ | |
local remote_file="$SSH_USER@$SSH_SERVER:$FULL_BACKUP_PATH/$1" | |
local dest_path=$2 | |
echo "Copying remote file $remote_file to $dest_path" | |
scp $remote_file $dest_path | |
} | |
function backup_exists() | |
{ | |
local backup_file=$1 | |
if [[ "$backup_file" =~ ".tar.gz$" ]]; then | |
return 0 | |
elif [[ "$backup_file" =~ ".sql.gz$" ]]; then | |
return 0 | |
fi | |
return 1 | |
} | |
# FIXME: Customize this function as | |
function delete_old_backup_file() | |
{ | |
local backup_file=$1 | |
local only_dirs=$2 | |
local backup_dir | |
if [[ -f $backup_file ]] && [[ "$backup_file" =~ ".tar.gz$" ]]; then | |
# FIXME: guessing backup_file it's a compressed directory with same name | |
# backup_dir="${backup_file%%.*}" | |
# echo "running rm dir -rf $backup_dir" | |
rm -rf var/WebApps #FIXME: must be configurable | |
if [[ -z $only_dirs ]]; then | |
rm $backup_file | |
fi | |
elif [[ -f $backup_file ]] && [[ "$backup_file" =~ ".sql.gz$" ]]; then | |
uncompressed_file="${backup_file%.*}" | |
if [[ -z $only_dirs ]]; then | |
rm $uncompressed_file | |
fi | |
fi | |
} | |
echo "Trying to download backups for date: $DATE" | |
if ! (ssh $SSH_USER@$SSH_SERVER "[[ -d $FULL_BACKUP_PATH ]]");then | |
echo "Backup directory $FULL_BACKUP_PATH doesn't exist on remote server" | |
exit 1 | |
fi | |
if [[ ! -d $LOCAL_BACKUPS_PATH ]]; then | |
echo "Local directory $LOCAL_BACKUPS_PATH doesn't exist" | |
exit 1 | |
fi | |
lunchy stop $DB_NAME | |
cd $LOCAL_BACKUPS_PATH | |
for backup_file in $BACKUP_FILES; do | |
if backup_exists $backup_file; then | |
erase_local="" | |
vared -p "Backup file $backup_file exists locally. Erase it? (y/n): " erase_local | |
if [[ $erase_local = "y" ]] || [[ $erase_local = "Y" ]]; then | |
delete_old_backup_file $backup_file | |
copy_remote_file $backup_file $LOCAL_BACKUPS_PATH | |
else | |
delete_old_backup_file $backup_file "only_dirs" | |
fi | |
fi | |
uncompress_and_restore_file $backup_file | |
done | |
lunchy start $DB_NAME | |
echo "Done." | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment