-
Notifications
You must be signed in to change notification settings - Fork 13
/
backup.sh
86 lines (72 loc) · 3.05 KB
/
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
#!/bin/sh
# https://github.com/dlabey/Simple-Linux-Bash-Rotating-Backup-Script
# Local Source
SOURCE=/full/path
# Local Destination
DESTINATION=/full/path
# Database Backup User
DATABASE=''
DATABASE_USER=''
DATABASE_PASSWORD=''
DATABASE_HOST=''
# DO NOT EDIT ANYTHING BELOW THIS
# Date Variables
DAY_OF_YEAR=$(date '+%j')
DAY_OF_MONTH=$(date '+%d')
DAY_OF_WEEK_RAW=$(date '+%w')
DAY_OF_WEEK=$((DAY_OF_WEEK_RAW + 1))
MONTH=$(date '+%m')
YEAR=$(date '+%Y')
# Make Temporary Folder
mkdir `dirname $0`/tmp
echo 'Made temporary folder...'
# Make Weekly Folder
mkdir `dirname $0`/tmp/weekly
echo 'Made weekly folder...'
# Make Folder For Current Year
mkdir `dirname $0`/tmp/${YEAR}
echo 'Made folder for current year...'
# Make Folder For Current Month
mkdir `dirname $0`/tmp/${YEAR}/$MONTH
echo 'Made folder for current month...'
# Make Biannual Folder For Current Year
mkdir `dirname $0`/tmp/${YEAR}/biannual
echo 'Made biannual folder for current year...'
# Make The Weekly Backup
tar -zcvf `dirname $0`/tmp/weekly/${DAY_OF_WEEK}_files.tar.gz $SOURCE
mysqldump -h $DATABASE_HOST -u $DATABASE_USER -p$DATABASE_PASSWORD $DATABASE > `dirname $0`/${DAY_OF_WEEK}.sql
tar -zcvf `dirname $0`/tmp/weekly/${DAY_OF_WEEK}_database.tar.gz `dirname $0`/${DAY_OF_WEEK}.sql
rm -rf `dirname $0`/${DAY_OF_WEEK}.sql
echo 'Made weekly backup...'
# Check If It Is The 1st or 182nd Day Of The Year Then Make A Biannual Backup
# If It Is By Copying The Weekly Backup To The Biannual Folder For The Current
# Year
if [ $DAY_OF_YEAR -eq 1 -o $DAY_OF_YEAR -eq 182 ] ; then
if [ $DAY_OF_YEAR -eq 1 ] ; then
cp `dirname $0`/tmp/weekly/${DAY_OF_WEEK}_files.tar.gz `dirname $0`/tmp/${YEAR}/biannual/01_files.tar.gz
cp `dirname $0`/tmp/weekly/${DAY_OF_WEEK}_database.tar.gz `dirname $0`/tmp/${YEAR}/biannual/01_database.tar.gz
fi
if [ $DAY_OF_YEAR -eq 182 ] ; then
cp `dirname $0`/tmp/weekly/${DAY_OF_WEEK}_files.tar.gz `dirname $0`/tmp/${YEAR}/biannual/02_files.tar.gz
cp `dirname $0`/tmp/weekly/${DAY_OF_WEEK}_database.tar.gz `dirname $0`/tmp/${YEAR}/biannual/02_database.tar.gz
fi
echo 'Made biannual backup...'
fi
# Check If It Is The 1st Or 14th Day Of The Month Then Make A Bimonthly Backup
# If It Is By Copying The Weekly Backup To The Folder For The Current Month
if [ $DAY_OF_MONTH -eq 1 -o $DAY_OF_MONTH -eq 14 ] ; then
if [ $DAY_OF_MONTH -eq 1 ] ; then
cp `dirname $0`/tmp/weekly/${DAY_OF_WEEK}_files.tar.gz `dirname $0`/tmp/${YEAR}/${MONTH}/01_files.tar.gz
cp `dirname $0`/tmp/weekly/${DAY_OF_WEEK}_database.tar.gz `dirname $0`/tmp/${YEAR}/${MONTH}/01_database.tar.gz
fi
if [ $DAY_OF_MONTH -eq 14 ] ; then
cp `dirname $0`/tmp/weekly/${DAY_OF_WEEK}_files.tar.gz `dirname $0`/tmp/${YEAR}/${MONTH}/02_files.tar.gz
cp `dirname $0`/tmp/weekly/${DAY_OF_WEEK}_database.tar.gz `dirname $0`/tmp/${YEAR}/${MONTH}/02_database.tar.gz
fi
echo 'Made monthly backup...'
fi
# Merge The Backup To The Local Destination's Backup Folder
cp -rf `dirname $0`/tmp/* $DESTINATION
# Delete The Temporary Folder
rm -rf `dirname $0`/tmp
echo 'Made backup.'