aboutsummaryrefslogtreecommitdiffstats
path: root/backup_db.sh
blob: 256269586e0ec713cdcfbdf95c3bdbc78d087147 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/bash
#set -x

# main configuration
script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
config_file="$script_dir/main.cfg"

# remote and local directories
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
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"

# change folder for temporary storage
cd "$db_dir"

# get all databases
databases=$(nice -n 19 mysql -u root -N -e "show databases;" | grep -v "^information_schema$" | grep -v "^mysql$")

# handle each database
for database in $databases
do
   # dump and gzip database
   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/"

   # delete temporary file
   rm -f "$database.sql.gz"
done # for database in $databases