diff options
author | lookshe <github@lookshe.org> | 2014-10-05 17:12:35 +0200 |
---|---|---|
committer | lookshe <github@lookshe.org> | 2014-10-05 17:12:35 +0200 |
commit | 41995c1ef2906ccf93728fefc65fee1b29ac6404 (patch) | |
tree | 0c847478b768c33878b134ef96939003f75748d1 | |
parent | 230462cbce1e2ee7758e4b9224a1c83663c8b70c (diff) |
added script for databases
some little fixes in backup.sh
-rwxr-xr-x | backup.sh | 13 | ||||
-rwxr-xr-x | backup_db.sh | 71 |
2 files changed, 78 insertions, 6 deletions
@@ -25,9 +25,10 @@ function source_section { line_end=$(expr $(grep -n "$section_end" "$config_name" | cut -d: -f1) - 1) line_diff=$(expr $line_end - $line_start) - head -n $line_end "$config_name" | tail -n $line_diff > /tmp/backup.tmp - source /tmp/backup.tmp - rm -f /tmp/backup.tmp + tmp_file=$(mktemp) + head -n $line_end "$config_name" | tail -n $line_diff > "$tmp_file" + source "$tmp_file" + rm -f "$tmp_file" } # copy file with rsync to server and save last timestamp to logfile @@ -35,9 +36,9 @@ function rsync_server { filefrom="$1" dirto="$2" logfileentry="$3" - #nice -n 19 rsync -rle "ssh -i $keyfile" "$1" "$userserver:$dirto" + #nice -n 19 rsync -rle "ssh -i $keyfile" "$filefrom" "$userserver:$dirto" # limit the bandwith - nice -n 19 rsync --bwlimit=100000 -rle "ssh -i $keyfile" "$1" "$userserver:$dirto" + nice -n 19 rsync --bwlimit=100000 -rle "ssh -i $keyfile" "$filefrom" "$userserver:$dirto" ret=$? if [ $ret -ne 0 ] then @@ -46,7 +47,7 @@ function rsync_server { echo "$filefrom" | grep ".month." > /dev/null 2>&1 ret2=$? # on monthly backups and connection errors there should be no update of logfile - if [ $ret2 -ne 0 -a $ret -e 0 ] + if [ $ret2 -ne 0 -a $ret -eq 0 ] then sed -e "s/^$logfileentry .*$/${logfileentry} $backup_time/" "$logfile" > "$logfile.tmp" mv -f "$logfile.tmp" "$logfile" diff --git a/backup_db.sh b/backup_db.sh new file mode 100755 index 0000000..76c06bc --- /dev/null +++ b/backup_db.sh @@ -0,0 +1,71 @@ +#!/bin/bash +#set -x + +# main configuration +script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +config_file="$script_dir/main.cfg" +db_dir="$script_dir/db" +backupdirsingle=db + +# source a specified section from a specified config +function source_section { + config_name="$1" + section_name="$2" + section_start="^\[$section_name\]$" + section_end="^\[/$section_name\]$" + + line_start=$(grep -n "$section_start" "$config_name" | cut -d: -f1) + line_end=$(expr $(grep -n "$section_end" "$config_name" | cut -d: -f1) - 1) + line_diff=$(expr $line_end - $line_start) + + tmp_file=$(mktemp) + head -n $line_end "$config_name" | tail -n $line_diff > "$tmp_file" + source "$tmp_file" + rm -f "$tmp_file" +} + +# copy file with rsync to server and save last timestamp to logfile +function rsync_server { + filefrom="$1" + dirto="$2" + #nice -n 19 rsync -rle "ssh -i $keyfile" "$filefrom" "$userserver:$dirto" + # limit the bandwith + nice -n 19 rsync --bwlimit=100000 -rle "ssh -i $keyfile" "$filefrom" "$userserver:$dirto" + ret=$? + if [ $ret -ne 0 ] + then + echo "problem in rsync: $filefrom" + fi +} + +# general section from main config +source_section "$config_file" "general" + +cd "$db_dir" + +databases=$(nice -n 19 mysql -u root -N -e "show databases;" | grep -v "^information_schema$" | grep -v "^mysql$") +for database in $databases +do + nice -n 19 mysqldump -u root "$database" > "$database.sql" + nice -n 19 gzip "$database.sql" + + # mount for renaming + sshfs $userserver:$backupdir $backupdir_local -o IdentityFile=$keyfile -o IdentitiesOnly=yes + + # rotate old backups + for count in `seq 7 -1 2` + do + count_last=$(expr $count - 1) + rm -f "$backupdir_local/$backupdirsingle/$database.sql.gz.$count" + mv -f "$backupdir_local/$backupdirsingle/$database.sql.gz.$count_last" "$backupdir_local/$backupdirsingle/$database.sql.gz.$count" > /dev/null 2>&1 + done + rm -f "$backupdir_local/$backupdirsingle/$database.sql.gz.1" + mv -f "$backupdir_local/$backupdirsingle/$database.sql.gz" "$backupdir_local/$backupdirsingle/$database.sql.gz.1" > /dev/null 2>&1 + + # unmount + umount $backupdir_local + + rsync_server "$database.sql.gz" "$backupdir/$backupdirsingle/" + + rm -f "$database.sql.gz" +done |