backup.sh 2.72 KB
Newer Older
Igor Pečovnik's avatar
update    
Igor Pečovnik committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash
DATE=`date +%F`
DBBACKUPNAME="mysql-"$DATE
WEBBACKUPNAME="website-"$DATE
FILEBACKUPNAME="website-"$DATE


# change this
COPY_TO=/root/temp/$DATE
COPY_FROM=/var/www/clients
USER=
PASSWORD=
HOST=localhost
mkdir -p $COPY_TO

Igor Pecovnik's avatar
Igor Pecovnik committed
16
17
18

function database_backup ()
{
Igor Pecovnik's avatar
#3    
Igor Pecovnik committed
19
	if which mysql >/dev/null; then
20
	mysqldump -Q -q -e -R --add-drop-table -A -u $USER -p$PASSWORD > $COPY_TO/all.db
Igor Pecovnik's avatar
Igor Pecovnik committed
21
22
23
	for db in $(echo 'SHOW DATABASES;'|mysql -u$USER -p$PASSWORD -h$HOST|grep -v '^Database$'|grep -v "^performance_schema" |grep -v "^information_schema" |grep -v "^mysql"); 
	do
		mysqldump \
Igor Pečovnik's avatar
update    
Igor Pečovnik committed
24
25
26
              -u$USER -p$PASSWORD -h$HOST \
              -Q -c -C --add-drop-table --add-locks --quick --lock-tables \
              $db | gzip --best -c > $COPY_TO/$DBBACKUPNAME-$db.sql.gz;
Igor Pecovnik's avatar
Igor Pecovnik committed
27
28
		echo -e "[\e[0;32m o.k. \x1B[0m] \e[1;32m$1\x1B[0mDatabase $db backup"		
	done;
Igor Pecovnik's avatar
#3    
Igor Pecovnik committed
29
	fi
Igor Pecovnik's avatar
Igor Pecovnik committed
30
31
32
33
34
}


function web_backup ()
{
Igor Pecovnik's avatar
#4    
Igor Pecovnik committed
35
	if [[ -d /var/www ]]; then 
Igor Pecovnik's avatar
Igor Pecovnik committed
36
37
38
39
40
	echo -e "[\e[0;32m o.k. \x1B[0m] \e[1;32m$1\x1B[0mWebsites backup"
	for x in $(find $COPY_FROM -maxdepth 2 -name "web*" -type d -print0 | xargs -0)
	do
	tar -cpvzf $COPY_TO/$WEBBACKUPNAME-$(basename $x).tar.gz $x &> /dev/null
	done;
Igor Pecovnik's avatar
#4    
Igor Pecovnik committed
41
	fi
Igor Pecovnik's avatar
Igor Pecovnik committed
42
43
44
45
46
}


function conf_backup ()
{
Igor Pecovnik's avatar
#4    
Igor Pecovnik committed
47
48
49
50
51
52
	[[ -f /etc/init.d/scanbuttond ]] && /etc/init.d/scanbuttond stop
	[[ -f /etc/init.d/vpnserver ]] && /etc/init.d/vpnserver stop
	service transmission-daemon stop &> /dev/null
	service tvheadend stop &> /dev/null
	service cups stop &> /dev/null
	service samba stop &> /dev/null
Igor Pecovnik's avatar
Igor Pecovnik committed
53
	echo -e "[\e[0;32m o.k. \x1B[0m] \e[1;32m$1\x1B[0mConf files backup"
Igor Pecovnik's avatar
Igor Pecovnik committed
54
55
56
57
58
59
60
	# find only existing
	filename=filelist.txt
	tmpfilename=/tmp/filelist.txt
	touch tmpfilename
	IFS=$'\n'
	for next in `cat $filename`
	do
Igor Pecovnik's avatar
bugfix    
Igor Pecovnik committed
61
62
63
64
		if [[ $next != \#* ]]; then
			echo $next
			[[ -f $next || -d $next ]] && echo "$next" >> $tmpfilename
		fi
Igor Pecovnik's avatar
Igor Pecovnik committed
65
	done
Igor Pecovnik's avatar
#3    
Igor Pecovnik committed
66
	tar cvPfz $COPY_TO/$FILEBACKUPNAME-allfiles.tgz -T $tmpfilename --exclude='*.sock' &> /dev/null
Igor Pecovnik's avatar
#4    
Igor Pecovnik committed
67
68
69
70
71
72
	service samba start &> /dev/null
	service cups start &> /dev/null
	service tvheadend start &> /dev/null
	service transmission-daemon start &> /dev/null
	[[ -f /etc/init.d/scanbuttond ]] && /etc/init.d/scanbuttond start
	[[ -f /etc/init.d/vpnserver ]] && /etc/init.d/vpnserver start
Igor Pecovnik's avatar
Igor Pecovnik committed
73
74
75
76
77
}


function mail_backup ()
{
Igor Pecovnik's avatar
Igor Pecovnik committed
78
	if [[ -d /var/vmail ]]; then 
Igor Pecovnik's avatar
#4    
Igor Pecovnik committed
79
80
		service dovecot stop &> /dev/null
		service postfix stop &> /dev/null
Igor Pecovnik's avatar
Igor Pecovnik committed
81
82
		echo -e "[\e[0;32m o.k. \x1B[0m] \e[1;32m$1\x1B[0mMail backup"
		tar cvPfz $COPY_TO/$FILEBACKUPNAME-mail.tgz /var/vmail	
Igor Pecovnik's avatar
#4    
Igor Pecovnik committed
83
84
		service postfix start &> /dev/null
		service dovecot start &> /dev/null
Igor Pecovnik's avatar
Igor Pecovnik committed
85
	fi
Igor Pecovnik's avatar
Igor Pecovnik committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
}


function pkglist_backup ()
{
	echo -e "[\e[0;32m o.k. \x1B[0m] \e[1;32m$1\x1B[0mPackage list backup"
	aptitude search '~i !~M !~pstandard !~pimportant !~prequired' | awk '{print $2}' > $COPY_TO/installedpackages
}


# main app
database_backup
web_backup
conf_backup
mail_backup
pkglist_backup