Run a bash in your container:
#docker #exec #bash #container
sudo docker exec -i -t 96885e9e9a51 /bin/bash
96885e9e9a51
is container id. You have to find it yourself by using the below command:docker ps -a
#docker #exec #bash #container
Transactions in Redis
MULTI
, EXEC
, DISCARD
and WATCH
are the foundation of transactions in Redis
. They allow the execution of a group of commands in a single step, with two important guarantees:- All the commands in a transaction are serialized and executed sequentially. It can never happen that a request issued by another client is served in the middle of the execution of a Redis transaction. This guarantees that the commands are executed as a single isolated operation.
- Either all of the commands or none are processed, so a Redis transaction is also atomic. The EXEC command triggers the execution of all the commands in the transaction, so if a client loses the connection to the server in the context of a transaction before calling the MULTI command none of the operations are performed, instead if the EXEC command is called, all the operations are performed. When using the append-only file Redis makes sure to use a single write(2) syscall to write the transaction on disk. However if the Redis server crashes or is killed by the system administrator in some hard way it is possible that only a partial number of operations are registered. Redis will detect this condition at restart, and will exit with an error. Using the redis-check- aof tool it is possible to fix the append only file that will remove the partial transaction so that the server can start again.
Sample usage of the transaction:
> MULTI
OK
> INCR foo
QUEUED
> INCR bar
QUEUED
> EXEC
1) (integer) 1
2) (integer) 1
As it is possible to see from the session above, EXEC returns an array of replies, where every element is the reply of a single command in the transaction, in the same order the commands were issued.
In the next post we will talk about
WATCH
and DISCARD
commands too.#redis #transaction #multi #exec #discard #watch
Transactions in Redis part2
DISCARD
can be used in order to abort a transaction. In this case, no commands are executed and the state of the connection is restored to normal.We can discard a transaction like below:
> SET foo 1
OK
> MULTI
OK
> INCR foo
QUEUED
> DISCARD
OK
> GET foo
"1"
As you can see
foo
variable has not been incremented and value is set to 1 not 2.Optimistic locking using check-and-set:
WATCH
is used to provide a check-and-set (CAS) behavior to Redis
transactions.WATCHed
keys are monitored in order to detect changes against them. If at least one watched key is modified before the EXEC command, the whole transaction aborts, and EXEC returns a Null reply to notify that the transaction failed.WATCH mykey
val = GET mykey
val = val + 1
MULTI
SET mykey $val
EXEC
Using the above code, if there are race conditions and another client modifies the result of val in the time between our call to
WATCH
and our call to EXEC
, the transaction will fail.#redis #transaction #multi #exec #discard #watch
Transactions in Redis part3
In order to implement transaction in
Python
you need to use pipline
and there is no such a thing as exec
, multi
, etc.r = redis.Redis()
p = r.pipeline()
p.set("transError", var)
p.execute()
MULTI
, SET
, EXEC
sent when p.execute()
is called. To omit the MULTI/EXEC
pair, use r.pipeline(transaction=False)
.More info: http://redis-py.readthedocs.io/en/latest/#redis.Redis.pipeline
#python #redis #transaction #multi #exec
Delete files older than X days. You can use find command in order to find files with specific patterns in a specific directory and then remove those files:
If no units are specified, this primary evaluates to true if the difference between the file last modification time and the time find was started, rounded up to the next full 24-hour period, is n 24-hour periods.
True if the program named utility returns a zero value as its exit status. Optional arguments may be passed to the utility. The expression must be terminated by a semicolon (``;''). If you invoke find from a shell you may need to quote the semicolon if the shell would otherwise treat it as a control operator. If the string ``{}'' appears anywhere in the utility name or the arguments it is replaced by the pathname of the current file. Utility will be executed from the directory from which find was executed. Utility and arguments are not subject to the further expansion of shell patterns and constructs. SO BECAREFUL SEMICOLON IS NEEDED!
#find #rm #exec #mtime #remove_old_files #remove
BACKUP_DIR=/var/backup
# Number of days to keep backups
KEEP_BACKUPS_FOR=30 #days
find $BACKUP_DIR -type f -name "*.sql.gz" -mtime +$KEEP_BACKUPS_FOR -exec rm {} \;
-mtime n[smhdw]:
If no units are specified, this primary evaluates to true if the difference between the file last modification time and the time find was started, rounded up to the next full 24-hour period, is n 24-hour periods.
-exec utility [argument ...] ;
True if the program named utility returns a zero value as its exit status. Optional arguments may be passed to the utility. The expression must be terminated by a semicolon (``;''). If you invoke find from a shell you may need to quote the semicolon if the shell would otherwise treat it as a control operator. If the string ``{}'' appears anywhere in the utility name or the arguments it is replaced by the pathname of the current file. Utility will be executed from the directory from which find was executed. Utility and arguments are not subject to the further expansion of shell patterns and constructs. SO BECAREFUL SEMICOLON IS NEEDED!
#find #rm #exec #mtime #remove_old_files #remove