Update for prague
[project/infrastructure.git] / snapshot / snapshot.sh
1 #!/bin/bash
2
3 # Exit immediately on uninitialized variable or error, and print each command.
4 set -uex
5
6 # Run a snapshot phase. See below for how it is called. ${suffix} is the phase
7 # and used in filenames.
8 function snapshot {
9   # Allow skipping common sanitization. For CiviCRM and anything else
10   # non-Drupal.
11   if [ ! "${skip_common-}" ]; then
12     # Execute common SQL commands.
13     [ -f "snapshot/common${suffix}.sql" ] && mysql -o ${tmp_args} < "snapshot/common${suffix}.sql"
14     # Execute common SQL commands, but don't exit if they fail.
15     [ -f "snapshot/common-force${suffix}.sql" ] && mysql -f -o ${tmp_args} < "snapshot/common-force${suffix}.sql"
16   fi
17
18   # Skip if this sanitization and phase does not exit.
19   [ ! -f "snapshot/${sanitization}${suffix}.sql" ] && return
20   # Execute SQL for this sanitization and phase.
21   mysql -o ${tmp_args} < "snapshot/${sanitization}${suffix}.sql"
22
23   # Remove initial '.'
24   subdir=$(echo "${suffix}" | sed -e 's/^\.//')
25   # Store reduce with dev, they are the same level of sanitization.
26   if [ "${subdir}" = 'reduce' ]; then
27     subdir='dev'
28   fi
29   # Save the DB dump.
30   mysqldump --single-transaction --quick ${tmp_args} | sed -e 's/^) ENGINE=[^ ]*/)/' | bzip2 > "/var/dumps/${subdir}/${JOB_NAME}${suffix}-${BUILD_NUMBER}-in-progress.sql.bz2"
31   mv -v "/var/dumps/${subdir}/${JOB_NAME}${suffix}-${BUILD_NUMBER}-in-progress.sql.bz2" "/var/dumps/${subdir}/${JOB_NAME}${suffix}-${BUILD_NUMBER}.sql.bz2"
32   ln -sfv "${JOB_NAME}${suffix}-${BUILD_NUMBER}.sql.bz2" "/var/dumps/${subdir}/${JOB_NAME}${suffix}-current.sql.bz2"
33
34   # Remove old snapshots.
35   old_snapshots=$(ls -t /var/dumps/${subdir}/${JOB_NAME}${suffix}-[0-9]*.sql.{bz2,gz} | tail -n +2)
36   if [ -n "${old_snapshots}" ]; then
37     rm -v ${old_snapshots}
38   fi
39 }
40
41 function clear_tmp {
42   echo "DROP DATABASE ${tmp_db}; CREATE DATABASE ${tmp_db};" | mysql ${tmp_args}
43 }
44
45 # Configure credentials
46 db_name=$1
47 db_user=$2
48 db_pass=$3
49
50 # If the sanitization is not set, use the DB name.
51 [ "${sanitization-}" ] || sanitization=${db_name}
52 # If the DB host is not set, use db3-vip.
53 [ "${db_host-}" ] || db_host=db3-vip.drupal.org
54
55 tmp_db=drupal_sanitize
56 tmp_user=sanitize_rw
57 tmp_pass=$4
58 tmp_host=db3-vip.drupal.org
59 tmp_args="-h${tmp_host} -u${tmp_user} -p${tmp_pass} ${tmp_db}"
60
61 clear_tmp
62
63 # Make a copy of live.
64 mysqldump -h$db_host -u$db_user -p$db_pass --single-transaction --quick $db_name 2> "${WORKSPACE}/mysqldump-errors.txt" | gzip > "${WORKSPACE}/tmp.mysql.gz"
65 # Check for errors.
66 if [ -s "${WORKSPACE}/mysqldump-errors.txt" ]; then
67   rm "${WORKSPACE}/tmp.mysql.gz"
68   cat "${WORKSPACE}/mysqldump-errors.txt"
69   exit 1
70 fi
71 # Copy live to tmp database.
72 gunzip < "${WORKSPACE}/tmp.mysql.gz" | mysql -o ${tmp_args}
73 rm "${WORKSPACE}/tmp.mysql.gz"
74
75 # Save a copy of the schema.
76 mysqldump --single-transaction --quick ${tmp_args} -d --compact --skip-opt > "${WORKSPACE}/schema.mysql"
77
78 # Truncate all tables with cache in the name.
79 echo "SHOW TABLES LIKE '%cache%';" | mysql -o ${tmp_args} | tail -n +2 | sed -e "s/^\(.*\)$/TRUNCATE \1;/" | mysql -o ${tmp_args}
80 echo "SHOW TABLES LIKE 'civicrm_export_temp%';" | mysql -o ${tmp_args} | tail -n +2 | sed -e "s/^\(.*\)$/TRUNCATE \1;/" | mysql -o ${tmp_args}
81 echo "SHOW TABLES LIKE 'civicrm_import_job%';" | mysql -o ${tmp_args} | tail -n +2 | sed -e "s/^\(.*\)$/TRUNCATE \1;/" | mysql -o ${tmp_args}
82
83 # Snapshot in stages.
84
85 # Raw is nearly unsanitized, excpet for some keys. Git-dev uses this for emails.
86 suffix=.raw
87 snapshot
88
89 # A snapshot suitable for staging. We remove emails to avoid emailing people.
90 suffix=.staging
91 snapshot
92
93 # A snapshot suitable for dev. We remove all private information.
94 suffix=.dev
95 snapshot
96
97 # A smaller snapshot for taking up less space on dev.
98 suffix=.reduce
99 snapshot
100
101 clear_tmp