Suppose you have a directory on your server where precious files are stored but there are multiple users owners and you want to preserve them while backupping. Suppose also you want to mount an external sshfs or cifs share for backup, usually in a cloud system. You can resolve and do a secure backup using a loop filesystem created on the cloud share. Here how to do that. In our example we are creating a backup of the whole directory of a zimbra collaboration and email suite server. This is a famous and performant email server widely used.
Mounting external cloud share
first of all mount the external share with cifs:
mount -t cifs -o user=userXXX,pass=passYYY //yourbackupshare.com/backup /mnt/backup
or with sshfs (obvously it depends on how your share is). If you use sshfs, it’s necessary that you generate a openssl keypairs for simply connecting to the target without password authentication.
sshfs -o reconnect,allow_other,uid=0,gid=0 userXXX@yourbackupshare.com:/ /mnt/backup
Create image file on the cloud share
then create a sparse image on the share. This second step is necessary in order to be sure to successfully backup all filesystem users and permissions. Here we are supposing to create a 500GB filesystem. Infact the seek parameter is important because it creates for us the sparse image. This means that we are not going to fill every bit on the target with zeros. This command will take very few seconds to be executed.
Â dd if=/dev/zero of=/mnt/backup/backup.img bs=1 seek=500G count=1
now format the target
Now you are ready to go. Last step is mounting the new 500GB filesystem with a loop device. Here is the command
mount -o loop /mnt/backup/backup.img /mnt/backup2
in this way, you have in /mnt/backup2 the final filesystem ready to receive the backup data.
Executing rsync of all data preserving users/permissions
Backup can be finally achieved with a simple rsync command (here we are supposing to backup the entire zimbra/ directory of an email server suite as zimbra is) :
rsync -av –sparse /opt/zimbra/ /mnt/backup2/zimbra/
you can also run it even if the backup share is on another host, changing the command in this way.
rsync -av –sparse /opt/zimbra/ remote_host:/mnt/backup2/zimbra/
where “remote_host” is the hostname or IP of remote system where the backup loop filesystem is mounted. You can also add the flag –delete if you want to remove from target the files which are removed from the source. For a email system this is important because you usually dont want to backup emails which are removed from source.
All your backup is contained into the image file /mnt/backup/backup.img , this is very nice no?
At the end of sync you can unmount the filesystems with
Obviuosly the share and then the loop filesystem must be mounted again before executing the rsync again. There are more complicated ways to run a backup, but this is simple and easy to setup and let you keep in security your data. Why the backup to the cloud is important? Because you can connect easily the share to another host and restore all your files without trouble. In addition normally space of a cloud share can be raised or shrinked easily.