summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorngharo <ngharo@gmail.com>2017-02-05 13:43:25 -0600
committerGitHub <noreply@github.com>2017-02-05 13:43:25 -0600
commit1020227ec869b8aea511a1a01901368dd2a748da (patch)
tree36a4b6f5ee403c5a05dc3a0e3162dcadd9d811ee
parent9e432c6e46fbf992594895aaf14c31a3fa1210f2 (diff)
downloadtarsnap-script-1020227ec869b8aea511a1a01901368dd2a748da.tar.xz
tarsnap-script-1020227ec869b8aea511a1a01901368dd2a748da.zip
Create backup.sh
-rw-r--r--backup.sh44
1 files changed, 44 insertions, 0 deletions
diff --git a/backup.sh b/backup.sh
new file mode 100644
index 0000000..789e8a3
--- /dev/null
+++ b/backup.sh
@@ -0,0 +1,44 @@
+#!/bin/bash
+# used to prefix tarsnaps and email reports
+tag="ngha.ro"
+
+tarsnap=/usr/local/bin/tarsnap
+sites=(ngha.ro example.com example.org)
+dirs=(
+ /etc/
+ /root
+ /var/www
+)
+log=$(mktemp)
+mail_to="foo@example.com"
+mail_subject="[${tag}] Backup results"
+
+sql_dump() {
+ local site="$1"
+ local db="${site%.*}"
+ local dump="/var/www/${site}/${db}.sql"
+
+ printf "Dumping MySQL database '%s' to %s ... " "$db" "$dump"
+ mysqldump -R -u USER -pPASSWORD "$db" > "$dump"
+ ret_code=$?
+ chown root:root "$dump"
+ chmod 0600 "$dump"
+
+ result="OK"
+ [[ $req_code -eq 0 ]] || result="FAIL"
+ printf "%s\n" "$result"
+
+ return $ret_code
+}
+
+for site in "${sites[@]}"; do
+ sql_dump $site >> "$log" 2>&1 || mail_subject="${mail_subject} [SQL FAILURES]"
+done
+
+$tarsnap -c -f "${tag}-$(date +%d%m%y_%H:%M)" "${dirs[@]}" 2>&1 \
+ | tee -a "$log" | logger -t backup
+
+[[ ${PIPESTATUS[0]} -eq 0 ]] || mail_subject="${mail_subject} [TARSNAP FAILURES]"
+mail -s "$mail_subject" "$mail_to" < "$log"
+
+rm "$log"