From 1020227ec869b8aea511a1a01901368dd2a748da Mon Sep 17 00:00:00 2001 From: ngharo Date: Sun, 5 Feb 2017 13:43:25 -0600 Subject: Create backup.sh --- backup.sh | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 backup.sh 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" -- cgit v1.2.3