public static function restore_from_file($backup_dir, $backup_file)
 {
     if (mkdir($backup_dir) === false) {
         throw new Sns_Exception_Unavailable_Operation('Cannot create the directory ' . $backup_dir);
     }
     self::restore_item($backup_file, $backup_dir);
     $options = Sns_Option::get_locations();
     foreach ($options as $option => $to) {
         $item = $option == Sns_Option::DB ? $backup_dir . SNS_DS . 'wp_dump.sql' : $backup_dir . SNS_DS . $option . '.zip';
         if (file_exists($item)) {
             if ($option == Sns_Option::DB) {
                 Sns_Log::log_action('Restoring database');
                 try {
                     Sns_Backup::import_db($item, true);
                 } catch (Exception $e) {
                     Sns_Log::log_exception_obj($e);
                 }
                 Sns_Log::log_action('Restoring database', SNS_LOG_END);
             } else {
                 Sns_Log::log_action('Restoring item ' . $item);
                 try {
                     self::restore_item($item, $to);
                 } catch (Exception $e) {
                     Sns_Log::log_exception_obj($e);
                 }
                 Sns_Log::log_action('Restoring item ' . $item, SNS_LOG_END);
             }
         }
     }
     self::delete_dir($backup_dir);
 }
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);
    }
}
 public function save()
 {
     global $wpdb;
     $table_name = SNS_DB_PREFIX . 'backups';
     $options = Sns_Option::get_options(true);
     $option_list = array();
     foreach ($options as $option => $data) {
         if ($option == Sns_Option::FULL) {
             $option_list = array($option);
             break;
         }
         if ($option != Sns_Option::COUNT) {
             $option_list[] = $option;
         }
     }
     $destination = new Sns_Destination($this->type);
     $destinations = $destination->get_destinations();
     $destination_list = array();
     foreach ($destinations as $name => $dst) {
         if ($dst->status == Sns_Destination::SET) {
             $destination_list[] = $name;
         }
     }
     $info = json_encode(array('options' => $option_list, 'destinations' => $destination_list));
     $data = array('type' => $this->type, 'info' => $info, 'backup_date' => date('Y-m-d H:i:s'), 'hash' => $this->hash, 'filename' => $this->filename);
     $r = $wpdb->insert($table_name, $data);
     if ($r === false) {
         throw new Sns_Exception_DB_Error('Error on inserting into ' . $table_name . ' data: ' . json_encode($data));
     }
 }
                                    <li class="settings-item"><a href="#settings-tab-options">Options <?php 
echo SNS_PRO_TOOLTIP;
?>
</a></li>
                                    <li class="settings-item settings-item-middle"><a href="#settings-tab-notifications">Notifications <?php 
echo SNS_PRO_TOOLTIP;
?>
</a></li>
                                    <li class="settings-item settings-item-middle"><a href="#settings-tab-cloud">Cloud</a></li>
                                    <li class="settings-item settings-item-middle"><a href="#settings-tab-log">Log</a></li>
                                </ul>
                            </div>
                            <div class="settings-content">
                                <div id="settings-tab-options">
                                    <?php 
Sns_Option::draw();
?>
                                </div>
                                <div id="settings-tab-notifications">
                                    <?php 
Sns_Notification::draw();
?>
                                </div>
                                <div id="settings-tab-cloud">
                                    <div id="dropbox-block">
                                        <?php 
Sns_Dropbox::draw();
?>
                                    </div>
                                    <div id="ftp-block">
                                        <?php 
function sns_backup_manual_backup()
{
    @set_time_limit(0);
    register_shutdown_function('sns_register_shutdown');
    try {
        Sns_Log::log_msg('########PROCESS STARTED########' . PHP_EOL);
        $state = Sns_State::get_status();
        if ($state['status'] == Sns_State::STATUS_ACTIVE) {
            throw new Sns_Exception_Unavailable_Operation('There is an existing active process.Please wait.');
        }
        $stateData = array('status' => Sns_State::STATUS_ACTIVE, 'type' => Sns_State::TYPE_BACKUP, 'start_date' => date('Y-m-d H:i:s'));
        Sns_State::update($stateData);
        Sns_Checker::check();
        $locations = isset($_POST['locations']) ? $_POST['locations'] : array();
        $destination = new Sns_Destination(Sns_Backup::BACKUP_MODE_MANUAL);
        $destination->set_destinations($locations);
        $destination->save();
        $backup = new Sns_Backup(Sns_Backup::BACKUP_MODE_MANUAL);
        Sns_State::$currentBackupFilename = $backup->getFilename();
        $dest = 'local';
        if (count($locations)) {
            $dest .= ',' . implode(',', array_keys($locations));
        }
        $options = Sns_Option::get_options(true);
        foreach ($options as $option => $data) {
            if ($option == Sns_Option::FULL) {
                $option_list = array($option);
                break;
            }
            if ($option != Sns_Option::COUNT) {
                $option_list[] = $option;
            }
        }
        $options = implode(',', array_values($option_list));
        $engLogDecription = '----------------------------------------------------';
        $engLogDecription .= "\n" . ucfirst($stateData['type']) . ": " . $options;
        $engLogDecription .= "\nDestination: " . $dest . "\n";
        $engLogDecription .= "----------------------------------------------------\n";
        Sns_Log::log_msg($engLogDecription);
        Sns_Log::log_action('Backing up');
        $warns = $backup->backup();
        Sns_Log::log_action('Backing up', SNS_LOG_END);
        Sns_Log::log_msg("\n");
        $skipped_files = '';
        if (!empty($warns['not_readable'])) {
            $skipped_files .= '*********WARNING**********' . PHP_EOL;
            $skipped_files .= 'The following files are not readable and were excluded from backup package' . PHP_EOL;
            $i = 1;
            foreach ($warns['not_readable'] as $file) {
                $skipped_files .= $i . '. ' . $file . PHP_EOL;
                $i++;
            }
            Sns_Log::log_msg($skipped_files);
        }
        $stateData = array('status' => Sns_State::STATUS_FINISHED, 'type' => Sns_State::TYPE_BACKUP);
        Sns_State::update($stateData);
        Sns_Log::log_msg('########PROCESS ENDED########' . PHP_EOL);
    } catch (Exception $e) {
        Sns_Log::log_exception_obj($e);
        $ex_data = Sns_Exception_Handler::get_exception_data($e);
        $stateData = array('status' => Sns_State::STATUS_FAILED, 'type' => Sns_State::TYPE_BACKUP, 'msg' => $ex_data['status'] . ' : ' . $ex_data['msg']);
        Sns_State::update($stateData);
        Sns_Log::log_msg('########PROCESS ENDED########' . PHP_EOL);
        Sns_Log::report('backup');
    }
    die;
}