Esempio n. 1
0
int Backup(TCHAR *backup_filename)
{
    bool bZip = false;
    TCHAR dbname[MAX_PATH], source_file[MAX_PATH] = { 0 }, dest_file[MAX_PATH];
    HWND progress_dialog = NULL;
    SYSTEMTIME st;

    CallService(MS_DB_GETPROFILENAMET, _countof(dbname), (LPARAM)dbname);

    if (backup_filename == NULL) {
        int err;
        TCHAR *backupfolder, buffer[MAX_COMPUTERNAME_LENGTH + 1];
        DWORD size = _countof(buffer);

        bZip = options.use_zip != 0;
        backupfolder = Utils_ReplaceVarsT(options.folder);
        // ensure the backup folder exists (either create it or return non-zero signifying error)
        err = CreateDirectoryTreeT(backupfolder);
        if (err != ERROR_ALREADY_EXISTS && err != 0) {
            mir_free(backupfolder);
            return 1;
        }
        RotateBackups(backupfolder, dbname);

        GetLocalTime(&st);
        GetComputerName(buffer, &size);
        mir_sntprintf(dest_file, _T("%s\\%s_%02d.%02d.%02d@%02d-%02d-%02d_%s.%s"), backupfolder, dbname, st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, buffer, bZip ? _T("zip") : _T("dat"));
        mir_free(backupfolder);
    }
    else {
        _tcsncpy_s(dest_file, backup_filename, _TRUNCATE);
        if (!mir_tstrcmp(_tcsrchr(backup_filename, _T('.')), _T(".zip")))
            bZip = true;
    }
    if (!options.disable_popups)
        ShowPopup(dbname, TranslateT("Backup in progress"), NULL);

    if (!options.disable_progress)
        progress_dialog = CreateDialog(g_hInstance, MAKEINTRESOURCE(IDD_COPYPROGRESS), 0, DlgProcProgress);

    SetDlgItemText(progress_dialog, IDC_PROGRESSMESSAGE, TranslateT("Copying database file..."));

    mir_sntprintf(source_file, _T("%s\\%s"), profilePath, dbname);
    TCHAR *pathtmp = Utils_ReplaceVarsT(source_file);
    BOOL res = 0;
    if (bZip)
        res = MakeZip(pathtmp, dest_file, dbname, progress_dialog);
    else
        res = CopyFile(pathtmp, dest_file, 0);
    if (res) {
        if (!bZip) { // Set the backup file to the current time for rotator's correct  work
            FILETIME ft;
            HANDLE hFile = CreateFile(dest_file, FILE_WRITE_ATTRIBUTES, NULL, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
            GetSystemTime(&st);
            SystemTimeToFileTime(&st, &ft);
            SetFileTime(hFile, NULL, NULL, &ft);
            CloseHandle(hFile);
        }
        SendDlgItemMessage(progress_dialog, IDC_PROGRESS, PBM_SETPOS, (WPARAM)(100), 0);
        UpdateWindow(progress_dialog);
        db_set_dw(0, "AutoBackups", "LastBackupTimestamp", (DWORD)time(0));
        if (!options.disable_popups) {
            size_t dest_file_len = mir_tstrlen(dest_file);
            TCHAR *puText;
            if (dest_file_len > 50) {
                size_t i;
                puText = (TCHAR*)mir_alloc(sizeof(TCHAR) * (dest_file_len + 2));
                for (i = (dest_file_len - 1); dest_file[i] != _T('\\'); i--)
                    ;
                //_tcsncpy_s(dest_file, backup_filename, _TRUNCATE);
                mir_tstrncpy(puText, dest_file, (i + 2));
                mir_tstrcat(puText, _T("\n"));
                mir_tstrcat(puText, (dest_file + i + 1));
            }
            else
                puText = mir_tstrdup(dest_file);

            // Now we need to know, which folder we made a backup. Let's break unnecessary variables :)
            while (dest_file[--dest_file_len] != L'\\')
                ;
            dest_file[dest_file_len] = 0;
            ShowPopup(puText, TranslateT("Database backed up"), dest_file);
            mir_free(puText);
        }
    }
    else
        DeleteFile(dest_file);
    mir_free(pathtmp);

    DestroyWindow(progress_dialog);
    return 0;
}
Esempio n. 2
0
int Backup(TCHAR* backup_filename)
{
	TCHAR source_file[MAX_PATH] = {0}, dest_file[MAX_PATH] = {0};
	TCHAR* backupfolder,* pathtmp,* puText;
	HWND progress_dialog;
	DWORD start_time = GetTickCount64();
	int i;
	size_t dest_file_len;

	CallService(MS_DB_GETPROFILENAMET, MAX_PATH, (LPARAM)dbname);

	if (backup_filename == NULL)
	{
		int err = 0;

		SYSTEMTIME st;
		TCHAR buffer[MAX_COMPUTERNAME_LENGTH + 1 ];
		//GetComputerName (buffer , & size);
		DWORD size = sizeof(buffer);
		/* Buffer size is chosen large enough to contain any DNS name, not just MAX_COMPUTERNAME_LENGTH + 1 */
		/* characters. MAX_COMPUTERNAME_LENGTH is usually less than 32, but it varies among systems, so we  */
		/* cannot use the constant in a precompiled Windows agent, which is expected to work on any system. */

		backupfolder = Utils_ReplaceVarsT(options.folder);
		// ensure the backup folder exists (either create it or return non-zero signifying error)
		err = CreateDirectoryTree(backupfolder);
		if(err != ERROR_ALREADY_EXISTS && err != 0) {
			return 1;
		}

		GetLocalTime(&st);
		GetComputerName(buffer, &size);
		mir_sntprintf(dest_file, MAX_PATH, _T("%s\\%s_%02d.%02d.%02d@%02d-%02d-%02d_%s.bak"), backupfolder, dbname, st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, buffer);
		mir_free(backupfolder);
	}
	else
		lstrcpyn(dest_file, backup_filename, MAX_PATH);

	if(!options.disable_popups)
		ShowPopup(dbname, TranslateT("Backup in Progress"));

	if(!options.disable_progress) {
		progress_dialog = CreateDialog(hInst, MAKEINTRESOURCE(IDD_COPYPROGRESS), 0, (DLGPROC)DlgProcProgress);
		SetDlgItemText(progress_dialog, IDC_PROGRESSMESSAGE, TranslateT("Rotating backup files..."));
	}

	RotateBackups(progress_dialog, start_time);

	SetDlgItemText(progress_dialog, 0xDAED, TranslateT("Copying database file..."));
	SendMessage(progress_dialog, PBM_SETPOS, (WPARAM)(int)(0), 0);
	UpdateWindow(progress_dialog);

	mir_sntprintf(source_file, MAX_PATH, _T("%s\\%s"), profilePath, dbname);
	pathtmp = Utils_ReplaceVarsT(source_file);
	if (CopyFile(pathtmp, dest_file, 0))
	{
		SendMessage(progress_dialog, PBM_SETPOS, (WPARAM)(int)(100), 0);
		UpdateWindow(progress_dialog);
		DBWriteContactSettingDword(0, "AutoBackups", "LastBackupTimestamp", (DWORD)time(0));
		if(!options.disable_popups)
		{
			dest_file_len = lstrlen(dest_file);
			if(dest_file_len > 50)
			{
				puText = mir_alloc(sizeof(TCHAR) * (dest_file_len + 2));
				for(i = (int)dest_file_len - 1; dest_file[i] != _T('\\'); i--);

				lstrcpyn(puText, dest_file, i + 2);
				lstrcat(puText, _T("\n"));
				lstrcat(puText, dest_file + i + 1);
			}
			else
				puText = mir_tstrdup(dest_file);

			ShowPopup(puText, TranslateT("Database backuped"));
			mir_free(puText);
		}
	}
	else
		DeleteFile(dest_file);
	mir_free(pathtmp);

	DestroyWindow(progress_dialog);
	return 0;
}