backup.sh 2.64 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
Igor Pecovnik's avatar
Igor Pecovnik committed
20
21
22
	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
23
24
25
              -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
26
27
		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
28
	fi
Igor Pecovnik's avatar
Igor Pecovnik committed
29
30
31
32
33
}


function web_backup ()
{
Igor Pecovnik's avatar
#4    
Igor Pecovnik committed
34
	if [[ -d /var/www ]]; then 
Igor Pecovnik's avatar
Igor Pecovnik committed
35
36
37
38
39
	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
40
	fi
Igor Pecovnik's avatar
Igor Pecovnik committed
41
42
43
44
45
}


function conf_backup ()
{
Igor Pecovnik's avatar
#4    
Igor Pecovnik committed
46
47
48
49
50
51
	[[ -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
52
	echo -e "[\e[0;32m o.k. \x1B[0m] \e[1;32m$1\x1B[0mConf files backup"
Igor Pecovnik's avatar
Igor Pecovnik committed
53
54
55
56
57
58
59
	# 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
60
61
62
63
		if [[ $next != \#* ]]; then
			echo $next
			[[ -f $next || -d $next ]] && echo "$next" >> $tmpfilename
		fi
Igor Pecovnik's avatar
Igor Pecovnik committed
64
	done
Igor Pecovnik's avatar
#3    
Igor Pecovnik committed
65
	tar cvPfz $COPY_TO/$FILEBACKUPNAME-allfiles.tgz -T $tmpfilename --exclude='*.sock' &> /dev/null
Igor Pecovnik's avatar
#4    
Igor Pecovnik committed
66
67
68
69
70
71
	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
72
73
74
75
76
}


function mail_backup ()
{
Igor Pecovnik's avatar
Igor Pecovnik committed
77
	if [[ -d /var/vmail ]]; then 
Igor Pecovnik's avatar
#4    
Igor Pecovnik committed
78
79
		service dovecot stop &> /dev/null
		service postfix stop &> /dev/null
Igor Pecovnik's avatar
Igor Pecovnik committed
80
81
		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
82
83
		service postfix start &> /dev/null
		service dovecot start &> /dev/null
Igor Pecovnik's avatar
Igor Pecovnik committed
84
	fi
Igor Pecovnik's avatar
Igor Pecovnik committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
}


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