-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions
120 lines (103 loc) · 3.53 KB
/
functions
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
add_backup_extension() {
keyname=`/opt/farm/ext/keys/get-gpg-backup-key.sh`
if [ "$keyname" != "" ]; then
echo "$1.gpg"
else
echo "$1.gz"
fi
}
stream_handler() {
keyname=`/opt/farm/ext/keys/get-gpg-backup-key.sh`
if [ "$keyname" != "" ]; then
echo "gpg --encrypt --no-armor --recipient $keyname --batch"
else
echo "gzip -c9"
fi
}
target_file_name() {
source_dir=`echo $1 |tr '.' '_'`
echo $source_dir.tar |cut -c2- |tr '/' '_'
}
backup_directory() {
temp_dir=$1
target_dir=$2
source_dir=$3
if [ "$4" != "" ]; then
target_file=$4
else
target_file=`target_file_name $source_dir`
fi
target_full=`add_backup_extension $target_file`
if [ -d $source_dir ] && [ ! -h $source_dir ] && [ "`ls -A $source_dir`" != "" ]; then
if [ "$4" != "" ]; then
part1=`dirname $source_dir`
part2=`basename $source_dir`
tar cf - -C $part1 $part2 2>/dev/null |`stream_handler` >$temp_dir/$target_full
else
tar cf - $source_dir 2>/dev/null |`stream_handler` >$temp_dir/$target_full
fi
mv -f $temp_dir/$target_full $target_dir/$target_full 2>/dev/null
fi
}
backup_mysql() {
host=$1
port=$2
user=$3
pass=$4
prefix=$5
temp=$6
dest=$7
nolock=$8
skipfile=$9
if [ -d $temp ] && [ -d $dest ]; then
access="-h $host -P $port -u $user -p$pass"
if [ "$skipfile" != "" ] && [ -f $skipfile ]; then
dbs="`echo \"show databases;\" |mysql $access |grep -v ^Database |grep -v information_schema |grep -v performance_schema |grep -v ^sys\$ |grep -v ^mysql\$ |grep -vxFf $skipfile`"
elif [ -s /etc/mysql/.skip-backup ]; then
dbs="`echo \"show databases;\" |mysql $access |grep -v ^Database |grep -v information_schema |grep -v performance_schema |grep -v ^sys\$ |grep -v ^mysql\$ |grep -vxFf /etc/mysql/.skip-backup`"
else
dbs="`echo \"show databases;\" |mysql $access |grep -v ^Database |grep -v information_schema |grep -v performance_schema |grep -v ^sys\$ |grep -v ^mysql\$`"
fi
fname=`add_backup_extension $prefix-grants.sql`
mysql --batch --skip-column-names $access -e "SELECT DISTINCT CONCAT('SHOW GRANTS FOR \'', user, '\'@\'', host, '\';') AS query FROM mysql.user" | \
mysql $access | sed 's/\(GRANT .*\)/\1;/;s/^\(Grants for .*\)/## \1 ##/;/##/{x;p;x;}' |`stream_handler` >$temp/$fname
mv -f $temp/$fname $dest/$fname 2>/dev/null
if [ "$nolock" = "" ]; then
echo "FLUSH TABLES WITH READ LOCK;" |mysql $access
fi
for db in $dbs; do
fname=`add_backup_extension $prefix-$db.sql`
if [ "$nolock" != "" ]; then
mysqldump $access --quick --extended-insert --complete-insert --skip-add-drop-table --single-transaction --databases $db |`stream_handler` >$temp/$fname
else
mysqldump $access --quick --extended-insert --complete-insert --skip-add-drop-table --lock-tables --lock-all-tables --flush-logs --databases $db | \
`stream_handler` >$temp/$fname
fi
mv -f $temp/$fname $dest/$fname 2>/dev/null
done
if [ "$nolock" = "" ]; then
echo "UNLOCK TABLES;" |mysql $access
fi
fi
}
backup_postgres_remotedb() {
host=$1
port=$2
user=$3
pass=$4
db=$5
prefix=$6
temp=$7
dest=$8
rm -f /root/.pgpass
echo "$host:$port:$db:$user:$pass" >/root/.pgpass
chmod 0600 /root/.pgpass
export PGPASSFILE=/root/.pgpass
fname=`add_backup_extension $prefix-$db.sql`
# pg_dump from Postgres 9.x cannot handle 10.x databases and so on
# that's why avoid using pg_dump from OS (often stable=old) and use
# newer custom version instead
/opt/farm/ext/binary-pgdump/wrapper/pg_dump -p $port -h $host -U $user -d $db |`stream_handler` >$temp/$fname
mv -f $temp/$fname $dest/$fname 2>/dev/null
rm -f /root/.pgpass
}