function sns_configure_backup_db_data()
{
    global $wpdb;
    $table_name = SNS_DB_PREFIX . 'options';
    $options = $wpdb->get_results("SELECT COUNT(*) as `cnt` FROM {$table_name}", ARRAY_A);
    if ($options[0]['cnt'] == 0) {
        foreach (Sns_Option::get_options_list() as $option) {
            if ($option == Sns_Option::COUNT) {
                $val = intval(SNS_BACKUPS_MAX_COUNT);
            } else {
                $val = intval(Sns_Option::SET);
            }
            $wpdb->insert($table_name, array('option' => $option, 'value' => $val));
        }
    }
    $table_name = SNS_DB_PREFIX . 'settings_destinations';
    $destinations = $wpdb->get_results("SELECT COUNT(*) as `cnt` FROM {$table_name}", ARRAY_A);
    if ($destinations[0]['cnt'] == 0) {
        foreach (Sns_Destination::get_destinations_list() as $destination) {
            $wpdb->insert($table_name, array('name' => $destination));
        }
    }
    $table_name = SNS_DB_PREFIX . 'settings_ftp';
    $ftp = $wpdb->get_results("SELECT COUNT(*) as `cnt` FROM {$table_name}", ARRAY_A);
    if ($ftp[0]['cnt'] == 0) {
        $wpdb->insert($table_name, array('server' => '', 'port' => SNS_FTP_DEF_PORT));
    }
    $table_name = SNS_DB_PREFIX . 'state';
    $state = $wpdb->get_results("SELECT `status` FROM {$table_name}", ARRAY_A);
    if (empty($state)) {
        $wpdb->insert($table_name, array('type' => Sns_State::TYPE_BACKUP, 'status' => Sns_State::STATUS_NONE));
    } else {
        $data = array('status' => Sns_State::STATUS_NONE);
        Sns_State::update($data);
    }
}
function sns_backup_action()
{
    $proc = Sns_State::get_status();
    $scheduleState = Sns_State::get_status(Sns_Backup::BACKUP_MODE_SCHEDULE);
    if ($proc['status'] != Sns_State::STATUS_ACTIVE && empty($scheduleState)) {
        $backup = new Sns_Backup('schedule');
        $data = array('status' => Sns_State::STATUS_ACTIVE);
        Sns_State::update($data);
        try {
            $backup->backup();
        } catch (Exception $e) {
            Sns_Log::log_exception_obj($e);
        }
        $data = array('status' => Sns_State::STATUS_FINISHED);
        Sns_State::update($data);
    }
}
    public static function draw($only_records = false)
    {
        self::saveExternalZip();
        $history = self::get_history();
        $state = Sns_State::get_status();
        $disabled = $only_records && $state['status'] == Sns_State::STATUS_ACTIVE ? ' disabled="disabled" ' : '';
        if (!$only_records) {
            ?>
            <span class="menu-title">Your Backup History</span>
            <div class="menu-content">
                <div class="external-restore">
                    <input type="text" placeholder="Browse a backup file to restore from." class="form-control external-backup-input">
                    <div id="external-container">
                        <button id="external-browse" type="button" class="btn btn-default sns-action">Browse</button>
                        <button id="external-restore" type="button" class="btn btn-primary sns-action">Restore</button>
                    </div>
                </div>

                <div class="separator"></div>
                <div id="progressbar-restore"><div class="progress-label"></div></div>
<?php 
        }
        ?>
            <div class="records">
                <table class="table">
                    <?php 
        if (empty($history)) {
            echo 'Your history is empty';
        } else {
            ?>
                            <thead>
                                <th class="h-date">Backup date</th>
                                <th>Information</th>
                                <th class="h-actions">Actions</th>
                            </thead>
                            <tbody>
                    <?php 
            foreach ($history as $item) {
                ?>
                             <tr>
                                 <td><?php 
                echo date('M d, Y H:i', strtotime($item->backup_date));
                ?>
</td>
                                 <td>
                                     <?php 
                $info = json_decode($item->info, true);
                echo '<b>options:</b> ' . implode(', ', $info['options']) . '<br/>';
                echo '<b>destinations:</b> local' . (empty($info['destinations']) ? '' : ',') . implode(', ', $info['destinations']) . '<br/>';
                ?>
                                 </td>
                                 <td>
                                     <button type="button" class="btn btn-primary btn-restore sns-action" <?php 
                echo $disabled;
                ?>
 data-backup_id="<?php 
                echo $item->id;
                ?>
">Restore</button>
                                     <a href="<?php 
                echo SNS_BACKUPS_URL . $item->filename . '.zip';
                ?>
"><button type="button" class="btn btn-default btn-download sns-action" <?php 
                echo $disabled;
                ?>
 data-backup_id="<?php 
                echo $item->id;
                ?>
">Download</button></a>
                                     <button type="button" class="btn btn-danger btn-delete sns-action" <?php 
                echo $disabled;
                ?>
 data-backup_id="<?php 
                echo $item->id;
                ?>
"><span class="glyphicon glyphicon-remove"></span></button>
                                 </td>
                             </tr>
                    <?php 
            }
            ?>
                            </tbody>
                    <?php 
        }
        ?>
                </table>
            </div>
            <?php 
        if (!$only_records) {
            ?>
            </div>
        <?php 
        }
    }
function sns_backup_prepare_process()
{
    $stateData = array('status' => Sns_State::STATUS_READY_TO_START, 'type' => isset($_POST['type']) && $_POST['type'] == Sns_State::TYPE_RESTORE ? Sns_State::TYPE_RESTORE : Sns_State::TYPE_BACKUP);
    Sns_State::update($stateData);
    die;
}