⬆️ ⬇️

Use 7zip for data backup. Continuation

I publish the article at the request of a friend who does not yet have an account on Habré, if anyone has an extra invite, please share evgeny.sementsov [dog] gmail.com



The idea to write my own “file labors” was born after reading an article about using 7Zip for data backup .

It must be said that the idea of ​​“inventing a bicycle” came for a reason. At the initial stage, the use of an external tool was planned.

There were the following requirements, born of experience:





The closest of more or less suitable solutions - Cobian Backup, Amanda.

I think they will do in most cases. But not in ours. I forgot to say that we do not have the ability to deploy MSI packages using Active Directory group policy tools. We have been using SAMBA for a long time. Because To create alternative silent installers with the necessary settings stitched in and then shamanize with psexec (for silent installation under administrator rights), I did not want to put it mildly, then an obvious thought came to mind - why not try using those powerful tools that are already in your pocket? ? In general, a good language Javascript, wired into the depths of WSH, as well as a powerful free console archiver 7Zip.



From reasoning get down to business.

Let's start in stages.

')

1) How to determine when to which user to backup.



Those. make it so that the channel remains uncrowded during the day (for many, only mail folders occupy several gigabytes).

With some uncritical error, we can assume that it is best to distribute all the time equally among all users.

We assume that backups should be made from 10 to 16. So, on a samba server, we count the number of all users and allocate time intervals. To do this, you need to start the winbindd service.

So set_intervals_for_backup.sh .



#!/bin/sh



all=`wbinfo -u|wc -l`

wbinfo -u | awk -v all=$all 'BEGIN {i=0;}; {i++; j=int(360*i/all); h=10+int(j/60); m=j%60; print $1" "h" "m}' > /usr/samba/netlogon/intervals.txt



- :

andrey 10 2

boris 10 4

boriska 10 6

cidor 10 8

...




It is necessary to properly register the launch of this script in cron.



2) How to create backups.



In smb.conf, you need to register the launch of logon.bat for all users.

And in his turn:



start wscript \\sambaserver\netlogon\backups_v2.js



backups_v2.js :



//---------------------------------------------- ->

// smb-

var path_for_saving = "\\\\backupserver\\backup\\profiles\\";



// , ,

users = new Array();

users.push('fed');

users.push('sol');

users.push('lanis');

users.push('solod');

//---------------------------------------------- <-



// - ( )

function findU_andExit(thisUser)

{

d = new Date;

if (d.getDate() % 5 != 0)

return;

for (i = 0; i < users.length; i++) // 0,1,2,3,4 possible but it will be only in case "0"

if (users[i] == thisUser)

WScript.Quit();

}



//

function rand(vmin, vmax)

{

if (vmax) {

return Math.floor(Math.random() * (vmax - vmin + 1)) + vmin;

} else {

return Math.floor(Math.random() * (vmin + 1));

}

}



// "" , .. 9 "09".

function d_2(p)

{

var tmp = 0;

if (p)

tmp = (p < 10) ? "0"+p : p;

else

tmp = '00';

return tmp;

}



// ..

function get2FDate()

{

d = new Date();

s = d_2(d.getDate())+'.'+d_2(d.getMonth()+1)+'.'+d.getYear();

return s;

}



// WMI

WMI = GetObject("winmgmts:");

//

function lowPrior(procName)

{



result = WMI.ExecQuery("SELECT * FROM Win32_Process Where Name = '"+procName+"'");

var IDLE = 64;

var colCS = new Enumerator(result);

for (; !colCS.atEnd(); colCS.moveNext())

colCS.item().SetPriority(IDLE);

}

// ,

function countProc(procName)

{

result = WMI.ExecQuery("SELECT * FROM Win32_Process Where Name = '"+procName+"'");

var colCS = new Enumerator(result);

var j = 0;

for (; !colCS.atEnd(); colCS.moveNext())

j++;

return j;

}



//

var WshShell = WScript.CreateObject("WScript.Shell")

var FSO = new ActiveXObject("Scripting.FileSystemObject");

var WshProcEnv = WshShell.Environment("PROCESS");

//

var cn = WshProcEnv("COMPUTERNAME");

//

var un = WshProcEnv("USERNAME");

// -

// ,

// winbindd - HOMEWORK

// nsswitch.conf winbind

// LDAP - ldapsearch ( )

path_for_saving += un;



// -

findU_andExit(un);



//

var up = WshProcEnv("USERPROFILE");



// -------------------- ->

// -

var h1=0;

var m1=0;



ForReading = 1;

f_intervals = FSO.OpenTextFile('\\\\sambaserver\\netlogon\\intervals.txt', ForReading, false);



//

var re = new RegExp("("+un+")\\s(\\d+?)\\s(\\d+?)","ig");

while (!f_intervals.AtEndOfStream)

{

s = f_intervals.ReadLine();

if ((re.exec(s)) != null)

{

h1 = RegExp.$2 * 1;

m1 = RegExp.$3 * 1;

}

}

f_intervals.Close();



// , , 10 16

// , -

if (h1==0 && m1==0)

{

h1 = rand(10,15);

m1 = rand(0,59);

}



//

function chkDate()

{

d = new Date();

if (h1==d.getHours() && m1==d.getMinutes())

return true;

else

return false;

}



// -------------------- <-



// " "

strDesktop = WshShell.SpecialFolders("Desktop");

// " "

strMyDocuments = WshShell.SpecialFolders("MyDocuments");

// , , The Bat!, Thunderbird.

strAppData = up + "\\Application Data";

strTheBat = strAppData + "\\The Bat!";

strThunderBird = strAppData + "\\Thunderbird";

// QIP .

strQIP = up + "\\QIP";



//

var saved_dirs = '';

//

var saveFN = path_for_saving+"\\"+get2FDate()+'_from_'+cn+'.zip';

// ()

var tmp_saveFN = saveFN+'.1';



while (1)

{

// 5 -

WScript.Sleep(5000);

if (chkDate()) //

{

// ,

saved_dirs += "\""+strDesktop+"\" \""+strMyDocuments+"\"";



// - -

try

{

if (FSO.FileExists(tmp_saveFN))

FSO.DeleteFile(tmp_saveFN, true);

}

catch(e)

{

}



// QIP, ; , Thunderbird, The Bat!

if (FSO.FolderExists(strQIP))

saved_dirs += " \""+strQIP+"\"";

if (FSO.FolderExists(strThunderBird))

saved_dirs += " \""+strThunderBird+"\"";

else

{

if (FSO.FolderExists(strTheBat))

saved_dirs += " \""+strTheBat+"\"";

}



//

WshShell.Run("regedit /ea "+path_for_saving+"\\"+get2FDate()+"_HKCU.reg HKEY_CURRENT_USER", 0, false);



// - ( ),

// - 7Zip.

WshShell.Run("\\\\sambaserver\\netlogon\\7za.exe a -tzip "+tmp_saveFN+' '+saved_dirs+' -ssw -mx1 -mmt -mm=Deflate', 0, false);



// -

WScript.Sleep(7500);

lowPrior('7za.exe');



while (1)

{

// - 7za

// - 7za,

// , , , , , ,

// ..

WScript.Sleep(1000);

if (countProc('7za.exe') == 0)

{

try

{

// ,

if (FSO.FileExists(saveFN))

FSO.DeleteFile(saveFN, true);

FSO.MoveFile(tmp_saveFN, saveFN);



}

catch(e)

{

}



// -

WScript.Quit();

}

}

}

}




3) How to manage the execution of backups.



I wrote a simple script for users.

backups_STOP_NOW.js :



var WMI = GetObject("winmgmts:");

var WshShell = WScript.CreateObject("WScript.Shell");



var IMname = "7za.exe";

var result = WMI.ExecQuery("SELECT * FROM Win32_Process Where Name = '"+ IMname + "'");

var j = 0;

var colCS = new Enumerator(result);

for (; !colCS.atEnd(); colCS.moveNext())

j++;



if (j>0)

{

var BtnCode = WshShell.Popup(" . ?", 0, " ", 4 + 32);

switch(BtnCode)

{

case 6: //yes

WshShell.Run("taskkill /IM " + IMname);

break;

}

}



4) How to manage storage with backups.



The amount of space on our server allowed us to store the last 3 backups.

Now I will give a script that runs from the admin machine, which clears the "extra" backups.

It must be elementarily shoved into the “Task Scheduler” and run under a user who has write rights in the folder with the backup storage.

clear_old_backups.js :



fso = new ActiveXObject("Scripting.FileSystemObject");



//

function diffDates(a1, a2)

{

//31.08.2009 => 20090831

a1 = fso.GetBaseName(a1);

a2 = fso.GetBaseName(a2);



var t1, t2;

t1 = a1.substr(6,4)+a1.substr(3,2)+a1.substr(0,2);

t2 = a2.substr(6,4)+a2.substr(3,2)+a2.substr(0,2);

t1 = t1 - 0; t2 = t2 - 0;

if (t2 > t1)

return 1;

else

return 0;

}

//

function ShowFolderList(folderspec)

{

var f, fc;

var p;



var ar;

var foldername = fso.GetFileName(folderspec);



// .zip .reg

// , ""

var r = new Array(/\.zip/i, /\.reg/i);



f = fso.GetFolder(folderspec);



//

// , ,

for (k in r) // for .zip, .reg

{

ar = new Array();

fc = new Enumerator(f.Files);



for (; !fc.atEnd(); fc.moveNext())

{

p = fc.item();

if (r[k].test(p))

ar.push(p);

}

// ,

// , ,

ar = (ar.toString()).split(",");



//

var tmp;

for (i=0; i < ar.length-1; i++)

for (j=i+1; j < ar.length; j++)

if (diffDates(ar[i],ar[j]))

{

tmp = ar[i];

ar[i] = ar[j];

ar[j] = tmp;

}



// 3

for (i=3; i < ar.length; i++) //0, 1, 2 will be

{

try

{

fso.DeleteFile(ar[i], true);

}

catch(e)

{

}

}

}

}



// ""

var d = fso.GetFolder("\\\\backupserver\\backup\\profiles");



fc = new Enumerator(d.SubFolders);

for (; !fc.atEnd(); fc.moveNext())

{

ShowFolderList(fc.item());

}




5) Summing up

So, in the end, we achieved exactly what we wanted. Backups are made in the .zip format, which can be opened by any archiver program.

I hope problems with search 7za.exe on a site 7-zip.org you will not have.

In our case, the user himself can get the necessary files from the backup, which (in the absence of admins) is also a plus.



Waiting for your comments! This is my first post on Habré and hopefully not the last. Unfortunately, I do not have an account and I cannot write comments (for the current moment).

Source: https://habr.com/ru/post/81657/



All Articles