Tech C**P
15 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
Sometimes you have to send a very large file over network to a remote destination, or want to copy a huge file to a partition which is mounted by NFS (Network File System). This issue will eat up all you network bandwidth until the copy process is finished, it can take an hour or more depending on the file size being copied.

This issue can be an ISSUE when it's a production server and your server is already over load, to copy a file you can use rsync command to limit your bandwidth and prevent network hogs:

rsync --bwlimit=1000 /var/www/html/ backups@server1.example.com:~/mysite


--bwlimit is in KB so the above example puts limit of 1000 Kilo Bytes on your copy command.

#rsync #bwlimit #NSF
There are times you run a command in cronjob in a specific interval. Let's say you run that command every hour.

If your command copies a huge file, or you are doing a heavy task that may take longer than 1 hour sometimes, then you need run-one
command in your arsenal.

run-one: run just one instance at a time of some command and unique set of arguments (useful for cronjobs, eg)


Sample run-one command:

run-one rsync -azP $HOME $USER@example.com:/srv/backup

See more samples here:
- http://manpages.ubuntu.com/manpages/xenial/man1/run-one.1.html


#linux #cronjob #command #run_one #runone
tarfile is a python library to read and write gzip`/`bz2 compressed files.

How to read a gzip compressed tar archive and display some member information:

import tarfile
tar = tarfile.open("sample.tar.gz", "r:gz")
for tarinfo in tar:
print tarinfo.name, "is", tarinfo.size, "bytes in size and is",
if tarinfo.isreg():
print "a regular file."
elif tarinfo.isdir():
print "a directory."
else:
print "something else."
tar.close()


Create a compressed file:

with tarfile.open(dst, "w:gz") as tar:
print("Archiving " + src + " into " + dst)
tar.add(src, arcname = os.path.basename(src))

NOTE: the flag of w:gz opens the destination in write mode. Used to create a new tar file.

#python #tarfile #tar #bz2 #gzip
Linux in Docker: Wheezy: "ps: command not found"

Solution:

RUN apt-get update && apt-get install -y procps


#docker #linux #ps #command_not_found #procps
How to go to the previous working directory in terminal?

cd -

#linux #cd #last_working_directory
How do you delete all text above a certain line?

dgg

Will delete everything from your current line to the top of the file.

#vim #delete #dgg
How to check expiration time of a PEM certificate using openssl?

$ openssl x509 -enddate -noout -in file.pem
notAfter=Sep 3 02:23:50 2018 GMT

#openssl #expiration_date
Today I had a problem on nginX. I don't know where to start! :|

Fair enough, this is my nginx stanza:

location /geo {
add_header 'Access-Control-Allow-Origin' '*';
if ( $arg_callback ) {
echo_before_body '$arg_callback(';
echo_after_body ');';
}

proxy_pass https://api.example.com/geo;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
}

NOTE: each part of nginX block is called stanza. I bet you didn't know about this one! :D

echo_before_body command will prepend something to the response which will be returned from nginX.

echo_after_body will append something to the response.

proxy_pass will proxy your requests to a backend server.

$arg_callback will get value of parameter callback in URL. So for example if you use $arg_type, you will get the value of type argument which is provided in URL: http://sample.com?type=SOMETHING

So far so good. The problem was that when I give call the URL with callback parameter https://api.example.com/geo?callback=test. It
generates /geo/geo URL instead of /geo. To circumvent the issue I used $request_uri in proxy_pass section proxy_pass https:// api.fax.plus$request_uri;. The route should be OK now, but there is one big problem here now that responses are returned in binary format instead of JSON. I removed Upgrade & Connection & proxy_http_version lines and it worked like a charm!

Don't ask me! I don't know what are Upgrade and Connection headers.

The output is like the below response for a URL similar to http://api.example.com/geo?callback=test:

test(
{
"username": "alireza",
"password": "123456"
}
)

#nginx #stanza #proxy_pass #echo_before_body #echo_after_body