Tech C**P
15 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
Log rotatation in Linux is so handy as its name implies, it rotates log files. If you have a look at the /var/log path you can see
some compressed files some files which ends with .1

/var/log/my-app.log {
weekly
rotate 4
compress
delaycompress
missingok
notifempty
create 644 root root
}

The location of log rotation configs is in /etc/logrotate.d/. I have created a file in it with the config above.

Explanation of some parameters:
- weekly: weekly says that you want to Linux to rotate your log files weekly, you can also set daily, monthly, yearly.

- rotate 4: it says that how many rotated files should be kept, here I keep 4 rotated files (one month).

- compress: well you tell me what this parameter does.

- delaycompress: some programs do not close file handlers immediately, here we tell log rotate to delay the compression.

- missingok: don't return error if the log file is missing

OK, the list goes on. Take a look at the manual yourself and see its options.

#linux #logrotate #rotate
Don't crack! I'd like to talk a little bit of Angular here rather than backend and those jargons :)

As you might know, Angular bind elements to component variables in order to update values. There is concept for data binding which is called Banana in a box and its form is like [()] (a banana in a box). This is a two-way binding mechanism. But why it is like that. Why a banana in a box? :)

In Angular to bind properties to your component variables use [] like below:

<img [src]="my_source_image" />

On the contrary in order to bind an event to an element you would use () as below:

<button (click)="doSomething()">

In two-way binding you do as below in reality:

<input [ngModel]="ctrl.name" (ngModelChange)="ctrl.name=$event">

NOTE: you bind name to ngModel and on model change you set event value on form control name: ctrl.name=$event.

This is why Angular uses `[()]. It binds both properties and events in one go like a magic.


#angular #bind #event_binding #two_way_binding #banana_in_a_box
How to create a an application for your platform? Let's say an EXE file for windows and .app for MAC or a binary for linux.

I suppose you have nodejs and npm already installed. You need to install nativefier node module using the command below first:

npm install -g nativefier

Now you can generate a native app from your website using the command below:

nativefier "https://www.google.com/" --name="Google"

There are many parameters you can give to nativefier in order to set custom settings on it. Check nativefier --help for that.

#nativefier #nodejs #npm #electron #mac #windows #exe
I have a script that checks a source folder for new files in case there are files in the source folder, it will move those files to destination.

The problem I encountered recently was that files are huge and it may be in the middle of the copying into source by another process so my script tries to move an incomplete file to a destination. Let's say the file is 4GB in size and just only 1GB of the file has been copied. I have to wait until file is 4GB and other handler using that file, then I should safely move the file.

You can use lsof command in order to check which processes are using the source file:


if [[ `lsof -- /var/my-folder/my-big-file.tar.gz` ]]
then
echo "File is being used by a process."
exit 1
fi


NOTE: you can give file directly to lsof using -- or you can use grep command as follow:


lsof | grep /var/my-folder/my-big-file.tar.gz


NOTE2: if you are in a loop use break instead of exit.

NOTE3: if you get command not found, install it using apt-get install lsof

#linux #sysadmin #lsof #grep
How to zero-pad a number in bash?

printf is here to help :)

In order to zero-pad a number you need to use do like below:

your_number_var=1
output=$(printf "%02d" $your_number_var)
echo $output # 01

Here I have used %02d. the number 2 refers to numbers of padding and d refers to digit. So to zero-pad to 5 you can use %05d.
As simple as that.

#bash #printf #zeropad #zero-pad #zeropadding
Tech C**P
https://blog.shippable.com/why-we-moved-from-nosql-mongodb-to-postgressql #mongoDB #posgreSQL
سایت shippable به دلایلی که در لینک بالا عنوان کردند مجبور به انتقال اطلاعات از مونگو به postgreSQL شدند. اما بنده در کامنت زیر اشتباهاتشان بررسی کردم و بهشون پاسخ دادم:

Basic principles are neglected as you have tried to store structured data there in MongoDB! One of the biggest problems is that you had a delay in data retrieval and without knowing the culprit you just wiped the question out! MongoDB profilers are good enough in order to see what's going on under the hood or using iostat to see where is the bottleneck. You couldn't even detect the bottleneck and just rebuilt the server, a big mistake here too!

In order to keep data model consistent at a specific level in MongoDB you need to create a model to map data to it and return default values for field not present in the real document. Here you tried to solve this problem from mongoDB side and updated all data!!! Again a big big mistake here as updates are very costly and MongoDB would have to relocate documents in disk in case they don't fit there (here all data will get fragmented and data need to be compacted to help a little bit).
How to limit bandwidth of rsync linux command?

rsync is an awesome tool in order to move files via ssh into another server or from server to local system like what scp does but
far better in terms of features and incremental copy mechanism. To limit bandwidth use bwlimit like below:

The general form:

rsync --bwlimit=<kb/second> <source> <dest>

An example of usage with 2MB limit per second transfer:

rsync --bwlimit=2000 /backup/folder user@example-host:/remote/backup/folder/

#linux #sysadmin #rsync #bandwidth
How to upgrade Axigen mail server?

The procedure is dead simple! Mail server itself notifies you about the new updates in its admin panel. Head over to the given link and download the the appropriate file related to your OS. Usually a .run file.

1- First make it executable:

chmod +x axigen-10.2.2.x86_64.rpm.run

NOTE: here my version update may differ from yours.

2- Now stop Axigen.

3- Run the installer and go in an interactive way. JUST be sure to skip the Axigen post-install configuration wizard.

4- Now start Axigen.

NOTE: in case you want to take backup copy the folder /var/opt/axigen in a safe place. This path may differ based on your OS and your distro.

#mail_server #axigen #upgrade #update
How to remove a user on CentOS 6?

userdel my-target-user

In case you want to remove all the associated user files too use -r parameter:

userdel -r my-target-user

Yes! As simple as that.

#linux #sysadmin #userdel #user #centos
Have you heard about CORS (Cross Origin Resource Sharing)?

If you know it, but like me you don't know it as you should and every time the bowser gives you a new error in console you again get wandered in the wilderness, I'd suggest going to the awesome link below and read thoroughly line by line:

https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

NOTE: this article is fantastic! Believe you me. You will understand eveything you need to know exactly what is CORS and how to handle it properly.


#browser #mozilla #CORS #cross_origin #cross_origin_resource_sharing
An interesting thing in console of browsers:

You give ANY styling to console.log outputs! Here is how:

const style = [
'background: linear-gradient(to right, #5433ff, #20bdff, #a5fecb);',
'color: #fff',
'padding: 10px 20px',
'line-height: 35px'].join(';');

console.log('%cHi there!', style);

By using %c you can give styles to your text outputs.

#css #console #developer_tools #text_formatting
nginX by default does not set CORS headers for requests with error status codes like 401. There are 2 options here to add CORS headers:

1- If your nginX version is new you have the option of always to add to add_header:
add_header 'Access-Control-Allow-Origin' $http_origin always;

2- If you got error of invalid number of arguments in "add_header" directive, then use more_set_headers:
more_set_headers -s '401 400 403 404 500' 'Access-Control-Allow-Origin: $http_origin';

NOTE: when this error happens then in $.ajax or any similar method you wont have access to status codes. So be extremely catious!!!

#nginX #web_server #CORS #more_set_headers #add_header #ajax
How to upload a text content as a file in $.Ajax()?

FormData class is used to create a multipart/form-data inside of JS code. A sample code speaks thousand words:


var formData = new FormData();
var blob = new Blob([YOUR_CONTENT_HERE], { type: "text/html"});
formData.append("file", blob);


We have created a binary data from the text which is in the format of text/html, then I have appended the data as an input file with the name of file (which will be captured on server-side).

The Ajax part is utterly simple:


$.ajax({
type: 'POST',
url: 'https://www.example.com/storage',
data: formData,
processData: false,
contentType: false
}).done(function(data) {});


NOTE: DO NOT OMIT processData, contentType parameters.

#javascript #jQuery #ajax #FormData #Blob #upload
Today's Tech Term:

D2D: D2D is shorthand for Disk-To-Disk and refers to data backups using disks.

#tech_terms #D2D #disk_to_disk