Using overlayfs
Use overlayfs on the root of your server (server folder).
Make a read-only link to the server folder, from which you can back up the data while the real directory is covered
mkdir -p /path/to/ReadOnlyServerRoot
sudo mount --bind /path/to/serverRoot /path/to/ReadOnlyServerRoot -o remount,ro
Shutdown the server so that files and databases are properly closed
Protect actual server root, /path/to/serverRoot
, from write.
sudo mount -t overlayfs overlayfs /path/to/serverRoot -o rw,uppderdir=/path/to/changes,lowerdir=/path/to/serverRoot
The changed and newly added data is stored at /path/to/changes
. When a file is modified a new copy is made on /path/to/changes/path/to/file
and this file is updated.
Start the server
Now you can backup your data from /path/to/ReadOnlyServerRoot
Shutdown server after the backup is completed
Remove the cover
sudo umount /path/to/serverRoot
Use a synchronization software, such as rsync, to merge the change data into the server file.
rsync /path/to/changes /path/to/serverRoot
rm -rf /path/to/changes
Start the server
More information on on overlayfs's option can be found on this answer.
Steps 2, 4, 6, and 9 are optional, but highly recommended: At the time of creating and removing overlay on server folder it is highly recommended to shutdown your server (only seconds of downtime) for a successful backup as server may cache partial data on their memory and only a part of the data would be updated. As a result backup contains invalid/unusable data which in turn lead to lost data.
Using a file system snapshot
switch to a file system that support snapshot like btrfs
or lvm
Update
If overlayfs is not available use aufs
or unionfs
unionfs
mount -t unionfs -o dirs=/branch_rw=rw:/branch_ro=rounionfs/union
creates a Union in directory /union
with the branch directories /branch_rw
(writable) and /branch_ro
(read-only).
aufs
mkdir /tmp/dir1
mkdir /tmp/aufs-root
mount -t aufs -o br=/tmp/dir1:/tmp/dir2 none /tmp/aufs-root/
The first two commands created 2 new directories. The mount.aufs
is the command to mount the filesystem as Union mount.
The mount command, specifies it is going to union mount /tmp/dir1
and /tmp/dir2
under /tmp/aufs-root
. The directory /tmp/aufs-root
will have the content of both /tmp/dir1
and /tmp/dir2
.
mount: special device overlayfs does not exist
, but on my laptop it had worked. They both have ubuntu trusty with theserver
tasksel andubuntu-standard
package. – Jun 26 '14 at 10:26/serverRoot /path/to/ReadOnlyServerRoot
. It even know about it. – totti Jun 26 '14 at 10:33