Redis save and backup script

This is idea how you can backup the file RDB file.

Basicaly you need to set up two crontab processes. First must perform BGSAVE:

redis-cli bgsave

When this is call, Redis will perform background save. Process usually will finish couple of minutes later. At the end new RDB file will be created.

Second crontab must start 10-15 min later. It needs to move / copy / upload the file to different location. For simplicity here I will simulate a copy to different disk or NFS image.

cp /data/redis/dump.rdb /backup/redis.dump.rdb

Here someone may rise very important question:
What will happen, if Redis begin to save at the time we copy?

Answer is trivial - if you are using modern filesystem, everything is OK and nothing will happen.

This is because of the way Redis do save operation.
First Redis forks, then it saves under temporary file. After process is done, Redis deletes the old file and rename temporary file.

In modern filesystems such EXT2, EXT3, XFS, RaiserFS, NTFS etc., the file always will be copied normally even file is deleted, because delete do not remove the file, but rather remove the only the link to the file. For more information, you may refer to UNIX C unlink() function.

How to do daily backup file

If we just copy the file, today's file will replace yesterday's. This is not always a good idea.

If we want to keep daily backup we can add "date '+%a'" and to append it to the backup filename. The following example also includes configurable directories.


DAY=`date '+%a'`


Crontab set-up

For those who are not familiar with crontab, here you are the crontab script itself:

0  0 * * *	redis-cli bgsave

# Simple copy
15 0 * * *	cp /data/redis/dump.rdb /backup/redis.dump.rdb

# ... or call more complex script (example is commented)
#15 0 * * *	/scripts/

blog comments powered by Disqus

Articles library

Redis as session handler in PHP

How to use Redis for session handler in PHP in a way similar to msession or PostgreSQL sessions
Date: 2011-07

Tags: php system administration

Lazy starter script on Linux such Redhat CentOS or Fedora

How to quickly made Redis to start after Linux boot.
Date: 2011-07

Tags: system administration

Redis vs Memcached comparison

Here is basic comparison of Redis Memcached and Memcachedb.
Date: 2011-07

Tags: memcached

Why RDBMS and SQL are difficult...

...while sometimes NoSQL solution may be easier and way faster? This arcle deals with incremental counters and how they needs to be implemented properly in RDBMS-es such MySQL
Date: 2011-07
Updated: 2011-09-30

Tags: mysql programming anti-pattern

Seamless migration from one Redis server to another

How to use replication to migrate Redis server without service interruption
Date: 2011-07

Tags: system administration

Redis swap issue while save

Explanation why your server load average may spike and server to swap memory when running redis
Date: 2011-08-21

Tags: system administration

Amazon EC2 and Amazon ElastiCache service

Memcached at Amazon first impressions
Date: 2011-08-23

Tags: memcached cloud system administration

Understanding hash-max-zipmap-entries and 'hash of hashes' optimization

Explanation of config parameters such hash-max-zipmap-entries and how we can make huge memory savings in new Redis 2.2
Date: 2011-08-28

Tags: optimization php programming system administration

NoSQL database design example

This article explains how we made our article section
Date: 2011-09-02

Tags: mysql database design programming

Redis save and backup script

Simple backup script for Redis
Date: 2011-09-28

Tags: optimization system administration

Postgres 9.1 foreign data wrapper interface

Postgres 9.1 gives you ability to access data from different data sources including Redis
Date: 2011-10-20

Tags: memcached optimization mysql programming pgsql

Redis high traffic connection issue

Explanation on 'Cannot assign requested address' connection error
Date: 2011-11-12

Tags: optimization scaling system administration

Redis too many open files error on high traffic sites

Explanation on 'Accepting client connection: accept: Too many open files'
Date: 2011-12-21

Tags: memcached security system administration

PHP Redis bug with PHP 5.4

Description of bug PHP Redis bug
Date: 2012-07-02

Tags: php system administration

Case Study: Using Redis intersect on very large sets

Intersection of 2 x 120M MySQL records
Date: 2012-08-29

Tags: optimization cloud php mysql programming

Getting started with Python and Redis on CentOS 6.X

How to install and test Redis client for Python on CentOS
Date: 2013-03-21

Tags: python system administration

GeoIP in Redis

Put Maxmind's GeoIP in Redis for best berformance for cient apps written in PHP or Python
Date: 2013-03-24

Tags: optimization php scaling mysql programming python

Redis database file examination

Examination of Redis rdb file in order to find huge memory consumption
Date: 2013-08-23

Tags: optimization system administration

© Jul.2011 - 2019,
This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Cookie Policy.