-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoBackup.sh
More file actions
executable file
·69 lines (56 loc) · 1.55 KB
/
doBackup.sh
File metadata and controls
executable file
·69 lines (56 loc) · 1.55 KB
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
#!/usr/bin/env ash
main()
{
local DESTINATION="$(date +%Y%m%d)"
bak_prepare
bak_database
bak_custom
bak_compress
bak_cleanup
}
bak_prepare()
{
echo "Creating temporary backup directory"
mkdir -p "${DESTINATION}"
}
bak_database()
{
mkdir -p "${DESTINATION}/database"
local DATABASES=$(env | grep '^BACKUP_')
for DATABASE in $DATABASES; do
# db name is between everything before the first "=" but after the first "_"
local dbname=$(echo "${DATABASE}" | cut -d= -f1 | cut -d_ -f2-)
# db username is assumed to match db name for now
local dbuser="${dbname}"
# db password is everything after the first "="
local dbpass=$(echo "${DATABASE}" | cut -d= -f2-)
local filename="${dbname}.sql"
echo "Copying database '$dbname'"
mysqldump --user "$dbuser" --password="$dbpass" --host "${DB_HOST}" "$dbname" > "${DESTINATION}/database/${filename}"
done
}
bak_custom()
{
for FILEPATH in $(find /custom -type f); do
local filename=$(basename "${FILEPATH}")
local dirname=$(dirname "${FILEPATH}")
echo "Copying custom file ${FILEPATH}"
mkdir -p "${DESTINATION}/${dirname}"
cp "${FILEPATH}" "${DESTINATION}/${dirname}/${filename}"
done
}
bak_compress()
{
echo "Compressing backup"
local ARCHIVE="$(basename "${DESTINATION}").tar.gz"
tar -czf "${ARCHIVE}" "${DESTINATION}"
mv "${ARCHIVE}" /backup/
}
bak_cleanup()
{
echo "Clean up temporary files"
rm -rf "${DESTINATION}"
echo "Clean up files older than ${RETENTION_DAYS} days"
find /backup -mtime "+${RETENTION_DAYS}" -delete
}
main "$@"