Skip to content

Instantly share code, notes, and snippets.

@stp-che
Last active October 2, 2015 19:58

Revisions

  1. stp-che revised this gist Apr 10, 2014. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions backup.sh
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,5 @@
    #!/bin/sh
    cd ~/app_workspace
    d=`date +%Y%m%d`
    path=/home/keeper/backup
    pg_dump -U postgres -d my_database -n my_schema -f $path"dump"$d -O
    zip -jq $path"dump"$d".zip" $path"dump"$d
    zip -q $path"files"$d -r clients_files
    zip -jq $path"dump"$d".zip" $path"dump"$d
  2. stp-che revised this gist Apr 10, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions command
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    C:\cygwin\bin\sh.exe -l -c 'cd /path/to/loding/script/folder && ruby load_backup.rb'
  3. stp-che revised this gist Apr 10, 2014. 1 changed file with 41 additions and 21 deletions.
    62 changes: 41 additions & 21 deletions load_backup.rb
    Original file line number Diff line number Diff line change
    @@ -1,29 +1,49 @@
    require 'rubygems'
    require 'net/scp'
    require 'fileutils'
    require 'pony'

    @date = Time.now.strftime '%Y%m%d'
    @host = '10.0.0.2'
    @user = 'keeper'
    @pwd = 'password'
    @remote = '/home/keeper/backup/'
    @local = '/home/stp/backup/'
    BACKUP_DIR = "/path/to/backup"
    REPORT_DIR = "#{File.expand_path File.dirname(__FILE__)}/synch_reports/#{Time.now.strftime('%Y-%m-%d')}"
    REMOTE_SERVER = 'myserver'
    REMOTE_USER = 'user'
    REPORT_RECIPIENTS = '*******'

    def send_msg( subj, msg )
    Pony.mail :to => 'my@email.com', :from => 'my@email.com', :subject => subj, :body => msg
    end

    begin
    FileUtils.mkdir_p REPORT_DIR unless File.directory?(REPORT_DIR)

    Net::SCP.start( @host, @user, :password => @pwd ) do |scp|
    scp.download! "#{@remote}dump#{@date}.zip", "#{@local}dump#{@date}.zip"
    scp.download! "#{@remote}files#{@date}.zip", "#{@local}files#{@date}.zip"
    end
    PARTS_TO_SYNCH = {
    db: {remote: '/path/to/db/dumps', local: "#{BACKUP_DIR}/db"},
    file_storage: {remote: '/path/to/file/storage', local: "#{BACKUP_DIR}/file_storage"}
    }

    send_msg 'daily backup is OK', 'Backup has been downloaded successfully'

    rescue Net::SCP::Error => e
    PARTS_TO_SYNCH.each do |id, opts|
    file_out = "#{REPORT_DIR}/#{id}.log"
    file_err = "#{REPORT_DIR}/#{id}_err.log"
    command = "rsync -avzu #{REMOTE_USER}@#{REMOTE_SERVER}:#{opts[:remote]} #{opts[:local]}"
    puts command
    Process.wait spawn(command, out: file_out, err: file_err)
    end

    send_msg 'daily backup FAILED', "Something WRONG\nBackup files downloading failed."
    errors = false
    attachments = {}.tap do |h|
    Dir["#{REPORT_DIR}/*"].select{|f| File.file?(f) && File.size(f) > 0}.each do |f|
    errors = true if f =~ /_err/
    h[File.basename(f)] = File.read f
    end
    end

    end
    Pony.mail({
    to: REPORT_RECIPIENTS,
    from: '*******',
    subject: "backup report - #{errors ? 'WITH ERRORS' : 'OK'}",
    body: "backup report - #{errors ? 'WITH ERRORS' : 'OK'}",
    attachments: attachments,
    via: :smtp,
    via_options: {
    address: '****',
    port: '25',
    user_name: '******',
    password: '******',
    authentication: :plain,
    domain: "******"
    }
    })
  4. stp-che created this gist Apr 5, 2012.
    7 changes: 7 additions & 0 deletions backup.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    #!/bin/sh
    cd ~/app_workspace
    d=`date +%Y%m%d`
    path=/home/keeper/backup
    pg_dump -U postgres -d my_database -n my_schema -f $path"dump"$d -O
    zip -jq $path"dump"$d".zip" $path"dump"$d
    zip -q $path"files"$d -r clients_files
    29 changes: 29 additions & 0 deletions load_backup.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    require 'rubygems'
    require 'net/scp'
    require 'pony'

    @date = Time.now.strftime '%Y%m%d'
    @host = '10.0.0.2'
    @user = 'keeper'
    @pwd = 'password'
    @remote = '/home/keeper/backup/'
    @local = '/home/stp/backup/'

    def send_msg( subj, msg )
    Pony.mail :to => 'my@email.com', :from => 'my@email.com', :subject => subj, :body => msg
    end

    begin

    Net::SCP.start( @host, @user, :password => @pwd ) do |scp|
    scp.download! "#{@remote}dump#{@date}.zip", "#{@local}dump#{@date}.zip"
    scp.download! "#{@remote}files#{@date}.zip", "#{@local}files#{@date}.zip"
    end

    send_msg 'daily backup is OK', 'Backup has been downloaded successfully'

    rescue Net::SCP::Error => e

    send_msg 'daily backup FAILED', "Something WRONG\nBackup files downloading failed."

    end