This repository has been archived by the owner on Feb 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
magento_backup.sh
executable file
·145 lines (121 loc) · 4.15 KB
/
magento_backup.sh
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/bin/bash
#@version 0.3.0
#@author Holger Lösken <[email protected]>
# Configuration
projectName=MyProject
currentDir="$(pwd)"
backupDir="$currentDir/var/backups"
# The user and pass are stored in ~/.netrc on the web server
ftp_backup_host="11.22.33.44"
ftp_backup_dir="backups" # Put a "." to stay in the current folder
# Edit below only if you know what you are doing!
dbXmlPath="app/etc/local.xml"
{
host="$(xmllint --xpath 'string(//default_setup/connection/host)' $dbXmlPath)"
username="$(xmllint --xpath 'string(//default_setup/connection/username)' $dbXmlPath)"
password="$(xmllint --xpath 'string(//default_setup/connection/password)' $dbXmlPath)"
dbName="$(xmllint --xpath 'string(//default_setup/connection/dbname)' $dbXmlPath)"
}
usage()
{
echo "
Usage: $0 -t <database|files|basesystem> -m <1|0> -b <1|0>
-t: Backup type, either database only or files
-m: If enabled you can skip media files (media directory) from being backuped.
Files in includes/ and /var as well as .htaccess will always be excluded.
-b: If enabled the backup files will be moved to an external ftp server.
This can only be used with a .netrc file with username and password of the server.
" 1>&2;
exit 1;
}
ftp_backup()
{
f=$1
ftp $ftp_backup_host <<ENDFTP
cd $ftp_backup_dir
put $f
quit
ENDFTP
}
while getopts ":t:m:b:" o; do
case "${o}" in
t)
t=${OPTARG}
((t == database || t == files || t == basesystem)) || usage
;;
m)
m=${OPTARG}
((m == 1 || m == 0)) || usage
;;
b)
b=${OPTARG}
((b == 1 || b == 0)) || usage
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
backupType=$t
skipMedia=$m
makeFtpBackup=$b
fileName=$projectName-$(date +"%Y-%m-%d")
if [ "$backupType" == "database" ] || [ "$backupType" == "basesystem" ]; then
echo "----------------------------------------------------"
echo "Dumping MySQL to $fileName.sql.gz..."
mysqldump -h$host -u$username -p$password $dbName | gzip > $fileName.sql.gz
echo "Done!"
fi
if [ "$backupType" == "files" ] || [ "$backupType" == "basesystem" ]; then
echo "----------------------------------------------------"
echo "Archiving Files to $fileName.tar.gz..."
if [ $skipMedia == 1 ]; then
echo " -> Skipping media files (media directory)"
tar -zcf $fileName.tar.gz --exclude=var --exclude=includes --exclude=media * .htaccess
else
echo " -> Include media files (media directory)"
tar -zcf $fileName.tar.gz --exclude=var --exclude=includes * .htaccess
fi
echo "Done!"
echo "----------------------------------------------------"
fi
if [ "$backupType" == "database" ] || [ "$backupType" == "files" ] || [ "$backupType" == "basesystem" ]; then
echo "----------------------------------------------------"
if [ ! -d $backupDir ]; then
echo "$backupDir does not exist! Creating..."
mkdir -p $backupDir;
echo "Done!"
echo "----------------------------------------------------"
fi
echo "Moving file to backup dir $backupDir..."
if [ "$backupType" == "database" ] || [ "$backupType" == "basesystem" ]; then
echo " -> $fileName.sql.gz"
mv $fileName.sql.gz $backupDir
fi
if [ "$backupType" == "files" ] || [ "$backupType" == "basesystem" ]; then
echo " -> $fileName.tar.gz"
mv $fileName.tar.gz $backupDir
fi
echo "Done!"
echo "----------------------------------------------------"
else
usage
fi
if [ "$makeFtpBackup" == "1" ]; then
echo "Copying files to ftp backup server ($ftp_backup_host)..."
if [ "$backupType" == "database" ] || [ "$backupType" == "basesystem" ]; then
echo " -> $fileName.sql.gz"
cd $backupDir
ftp_backup "$fileName.sql.gz"
cd -
fi
if [ "$backupType" == "files" ] || [ "$backupType" == "basesystem" ]; then
echo " -> $fileName.tar.gz"
cd $backupDir
ftp_backup "$fileName.tar.gz"
cd -
fi
echo "Done!"
echo "----------------------------------------------------"
fi