backup.sh 2.44 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
19

function crontab_backup ()
{
	echo -e "[\e[0;32m o.k. \x1B[0m] \e[1;32m$1\x1B[0mCrontab backup"
Igor Pecovnik's avatar
Igor Pecovnik committed
20
	crontab -l > $COPY_TO/crontab-root.txt &> /dev/null
Igor Pecovnik's avatar
Igor Pecovnik committed
21
22
23
24
25
}


function database_backup ()
{
Igor Pecovnik's avatar
#3    
Igor Pecovnik committed
26
	if which mysql >/dev/null; then
Igor Pecovnik's avatar
Igor Pecovnik committed
27
28
29
	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
30
31
32
              -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
33
34
		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
35
	fi
Igor Pecovnik's avatar
Igor Pecovnik committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
}


function web_backup ()
{
	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;
}


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


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


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
crontab_backup
database_backup
web_backup
conf_backup
mail_backup
pkglist_backup