From anonymous, 6 Years ago, written in Bash.
Embed
  1. header() {
  2.     echo "========== $(date) - $@ =========="
  3. }
  4.  
  5. mysql_user="x"
  6. mysql_pwd="x"
  7.  
  8. ftp_user="x"
  9. ftp_pwd="x"
  10. ftp_host="x"
  11.  
  12. tmp=$(mktemp -d --tmpdir=/var/backups "$(date '+%Y-%m-%d-XXXXX')") || exit 1
  13. uploaddir=${tmp##*/}
  14.  
  15. status=0
  16. cd "$tmp"
  17.  
  18. #### SQL ####
  19. header "Dumping SQL"
  20. mysqldump --user="$mysql_user" --password="$mysql_pwd" --all-databases | \
  21.           gzip > "sql.gz"
  22. ret="$?"
  23. header "SQL finished (exit status $ret)"
  24. ((ret != 0)) && status=2
  25.  
  26. #### FILES ####
  27. header "Creating archive"
  28.  
  29. if [[ $(date +%u) == 7 ]]; then # Sunday, create full backup
  30.     rm -f /var/backups/last.snar
  31.     uploaddir+="-full"
  32. else
  33.     uploaddir+="-inc"
  34. fi
  35.  
  36. tar -g /var/backups/last.snar -cp --exclude-tag-all=.nobackup \
  37.     --exclude=tmp --exclude=mnt --exclude=proc --exclude=sys \
  38.     --exclude=dev --exclude /home/srv --exclude /var/cache / \
  39.     --exclude=run | pigz > "backup.tar.gz"
  40.  
  41. ret="$?"
  42. header "Archive finished (exit status $ret)"
  43. ((ret != 0)) && status=3
  44.  
  45. #### UPLOAD ####
  46. header "Beginning upload"
  47.  
  48. ncftp <<- EOT
  49.     open -u "$ftp_user" -p "$ftp_pwd" "$ftp_host"
  50.     mkdir "$uploaddir"
  51.     cd "$uploaddir"
  52.     put *
  53.     quit
  54. EOT
  55.  
  56. ret="$?"
  57. header "Upload finished (exit status $ret)"
  58. ((ret != 0)) && status=4
  59.  
  60. rm -f "$tmp"
  61.  
  62. exit $status