-
Notifications
You must be signed in to change notification settings - Fork 30
/
full_backup
executable file
·140 lines (115 loc) · 4.08 KB
/
full_backup
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
#!/bin/bash
# BEGIN CONFIGURATION ==========================================================
BACKUP_DIR="/backups/site_backups" # The directory in which you want backups placed
DUMP_MYSQL=true
TAR_SITES=false
SYNC="none" # Either 's3sync', 'rsync', or 'none'
KEEP_MYSQL="14" # How many days worth of mysql dumps to keep
KEEP_SITES="2" # How many days worth of site tarballs to keep
MYSQL_HOST="localhost"
MYSQL_USER="root"
MYSQL_PASS=""
MYSQL_BACKUP_DIR="$BACKUP_DIR/mysql/"
SITES_DIR="/var/www/sites/"
SITES_BACKUP_DIR="$BACKUP_DIR/sites/"
# See s3sync info in README
S3SYNC_PATH="/usr/local/s3sync/s3sync.rb"
S3_BUCKET="my-fancy-bucket"
AWS_ACCESS_KEY_ID="YourAWSAccessKey" # Log in to your Amazon AWS account to get this
AWS_SECRET_ACCESS_KEY="YourAWSSecretAccessKey" # Log in to your Amazon AWS account to get this
USE_SSL="true"
SSL_CERT_DIR="/etc/ssl/certs" # Where your Cert Authority keys live; for verification
SSL_CERT_FILE="" # If you have just one PEM file for CA verification
# If you don't want to use S3, you can rsync to another server
RSYNC_USER="user"
RSYNC_SERVER="other.server.com"
RSYNC_DIR="web_site_backups"
RSYNC_PORT="22" # Change this if you've customized the SSH port of your backup system
# You probably won't have to change these
THE_DATE="$(date '+%Y-%m-%d')"
MYSQL_PATH="$(which mysql)"
MYSQLDUMP_PATH="$(which mysqldump)"
FIND_PATH="$(which find)"
TAR_PATH="$(which tar)"
RSYNC_PATH="$(which rsync)"
# END CONFIGURATION ============================================================
# Announce the backup time
echo "Backup Started: $(date)"
# Create the backup dirs if they don't exist
if [[ ! -d $BACKUP_DIR ]]
then
mkdir -p "$BACKUP_DIR"
fi
if [[ ! -d $MYSQL_BACKUP_DIR ]]
then
mkdir -p "$MYSQL_BACKUP_DIR"
fi
if [[ ! -d $SITES_BACKUP_DIR ]]
then
mkdir -p "$SITES_BACKUP_DIR"
fi
if [ "$DUMP_MYSQL" = "true" ]
then
# Get a list of mysql databases and dump them one by one
echo "------------------------------------"
DBS="$($MYSQL_PATH -h $MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASS -Bse 'show databases')"
for db in $DBS
do
if [[ $db != "information_schema" && $db != "mysql" && $db != "performance_schema" ]]
then
echo "Dumping: $db..."
$MYSQLDUMP_PATH --opt --skip-add-locks -h $MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASS $db | gzip > $MYSQL_BACKUP_DIR$db\_$THE_DATE.sql.gz
fi
done
# Delete old dumps
echo "------------------------------------"
echo "Deleting old backups..."
# List dumps to be deleted to stdout (for report)
$FIND_PATH $MYSQL_BACKUP_DIR*.sql.gz -mtime +$KEEP_MYSQL
# Delete dumps older than specified number of days
$FIND_PATH $MYSQL_BACKUP_DIR*.sql.gz -mtime +$KEEP_MYSQL -exec rm {} +
fi
if [ "$TAR_SITES" == "true" ]
then
# Get a list of files in the sites directory and tar them one by one
echo "------------------------------------"
cd $SITES_DIR
for d in *
do
echo "Archiving $d..."
$TAR_PATH --exclude="*/log" -C $SITES_DIR -czf $SITES_BACKUP_DIR/$d\_$THE_DATE.tgz $d
done
# Delete old site backups
echo "------------------------------------"
echo "Deleting old backups..."
# List files to be deleted to stdout (for report)
$FIND_PATH $SITES_BACKUP_DIR*.tgz -mtime +$KEEP_SITES
# Delete files older than specified number of days
$FIND_PATH $SITES_BACKUP_DIR*.tgz -mtime +$KEEP_SITES -exec rm {} +
fi
# Rsync everything with another server
if [[ "$SYNC" == "rsync" ]]
then
echo "------------------------------------"
echo "Sending backups to backup server..."
$RSYNC_PATH --del -vaze "ssh -p $RSYNC_PORT" $BACKUP_DIR/ $RSYNC_USER@$RSYNC_SERVER:$RSYNC_DIR
# OR s3sync everything with Amazon S3
elif [[ "$SYNC" == "s3sync" ]]
then
export AWS_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY
export SSL_CERT_DIR
export SSL_CERT_FILE
if [[ $USE_SSL == "true" ]]
then
SSL_OPTION=' --ssl '
else
SSL_OPTION=''
fi
echo "------------------------------------"
echo "Sending backups to s3..."
$S3SYNC_PATH --delete -v $SSL_OPTION -r $BACKUP_DIR/ $S3_BUCKET:backups
fi
# Announce the completion time
echo "------------------------------------"
echo "Backup Completed: $(date)"