#!/usr/bin/env ruby
require "date"
require "fileutils"

$NOW = Date.today.strftime("%d-%m-%Y_%H:%M:%S")

# borg repository
$REPO = "/home/dany/backups/borg"

# websites folder
$WEBSITESDIR = "/home/dany/www"

# local backup folder for db files
$BACKUPDIR = "/home/dany/backups"

unless File.exist?( $BACKUPDIR )
  FileUtils.mkdir_p( $BACKUPDIR )
end


# Unterverzeichnisse von www die gebackuppt werden sollen
liste = [
    "conf", 
    "danletard.de", 
    "papier-restaurierung", 
    "schubertdaniel", 
    "x2ero", 
    "conworx", 
    "rvv/wp-content", 
    "rvv/shop"
  ]


# websites
def websites( liste )
  Dir.chdir( $WEBSITESDIR )

  liste.each do |elem|
    if File.exist?( elem )
      `borg create -C zlib,8 #{$REPO}::#{elem}-{now:%d-%m-%Y_%H:%M:%S} #{elem} --exclude '*mpeg'`

    else 
      puts "File or Directory \"#{elem}\" not found!\n" 
    end
  end

end

# davical db backup
def calendar_db_backup 
  Dir.chdir( $BACKUPDIR )
  `pg_dump -U davical_app davical | gzip > kalender-#{$NOW}.psql.gz`
end

# wordpress db backup
def  wp_db_backup 
  host= "localhost"
  user= "rvv"
  pass= "owUy9JTWLHQ0"
  #db= "rvv0rettung"
  db= "rvv_dev"

  do_sql_backup(host,user,pass,db)
end

# oxid db backup
def oxid_db_backup 
  host= "localhost"
  user= "rvv"
  pass= "owUy9JTWLHQ0"
  db= "rvv_shop"

  do_sql_backup(host,user,pass,db)
end

#
# Helper function
#
def do_sql_backup(host,user,pass,db)
  Dir.chdir( $BACKUPDIR )
  `mysqldump --opt --add-drop-table -h#{host} -u#{user} -p#{pass} #{db} | gzip > #{db}-#{$NOW}.sql.gz`
end

websites( liste )
wp_db_backup
oxid_db_backup
calendar_db_backup

FileUtils.chown_R( 1000, 1000, $BACKUPDIR)


