Skip to main content

Backup to Yandex.Disk via davfs

Backup is an important task that needs to be performed on any server where important data is located. It can be solved by various methods. For large systems, this is the appropriate software that can create backups without noticeable server latency.

For smaller systems (a la server with a dozen sites), the problem can be solved using simpler methods and using self-written scripts that copy data, for example, on Yandex.Disk. The advantage is that you do not need to spend money on an additional server (or special storage for backups, which some hosters offer). And ten gigabytes allocated by Yandex can be enough.

You can register a dedicated account in Yandex, or you can use a mailbox on your domain that runs on Mail for Domains.

The content of the article:

What do you need for such a backup?

All that is needed is the davfs2 package, through which the webdav section will be mounted in the system.

  # aptitude install davfs2 

Setting davfs

Its configuration files are located in the / etc / davfs2 folder. The davfs.conf file contains all available settings with default values. We will not touch him for now.

The second file is interesting - secrets, containing logins, passwords, proxy and certificate settings. More specifically, the “Credential Line” section, where you need to register an entry of the form:

  / mnt / yadisk vashparol 

The first argument is the mount point, the second is your Yandex mail (login), the third is the password. This is all that is required to specify.

Creating a mount point and entry in / etc / fstab

The section needs to be mounted somewhere. As seen in the example above, this is the / mnt / yadisk directory, which can be replaced with your own. Create it with root access only:

  # mkdir -m 0700 / mnt / yadisk 

Then we edit the file / etc / fstab, writing the line there: / mnt / yadisk davfs noauto, noexec, nosuid, file_mode = 0600, dir_mode = 0700 0 0 

Here, the first is the address to connect to Yandex.Disk, the second is the mount point, the third is the file system type, the fourth is options.

  • noauto - disable file system mounting when the OS starts, or with the command mount -a
  • noexec, nosuid - disables the launch of executable files and the installation of suid / sgid bits on files and folders.
  • file_mode / dir_mode - sets permissions for files and folders, respectively, only for the owner (root, if no user and group are specified in the uid and gid parameters

Why not mount the partition automatically? It is needed only during backup. And after it will be a) hang idle, b) will consume a small amount of traffic, synchronizing content.

After saving fstab, you can check whether the partition is mounted correctly with the command mount / mnt / yadisk .

Backup script

I have a few sites on the server. Basically, the content changes infrequently and you can use the full copy method.

For these purposes, wrote a small simple script on bash.

  #! / bin / bash
 # Initialize variables
 BACKUPDIR = "/ var / tmp" # Directory for backups
 TMPDIR = "backup" # Temporary folder for copies of files, which is then archived
 YADISK = "/ mnt / yadisk" # Mount Point
 SQLDIR = "sqldb" # Folder for databases
 SQLUSER = "root"
 SQLPASS = "12345"
 TIMEY = $ (date +% F_% H% M% S) # Time in format YEAR-MONTH-DAY_hour of a minute an second
 # We check the presence of a folder for databases, if it is not there, all the necessary are created.
 if [!  -d "$ BACKUPDIR / $ TMPDIR / $ SQLDIR"];  then
 mkdir -p -m 0700 "$ BACKUPDIR / $ TMPDIR / $ SQLDIR"
 # Connect Yandex.Disk
 mount "$ YADISK"
 # Create backups
 # 1 database
 for database in `mysql -s -r -e 'SHOW DATABASES' |  grep -v Database |  grep -v information_schema |  grep -v performance_schema |  grep -v mysql`;
 do mysqldump "$ database"> "$ database" .sql; done
 # 2 All users
 cd /
 tar -cf "$ BACKUPDIR" / "$ TMPDIR" /home.tar home
 # 3 system settings
 tar -cf "$ BACKUPDIR" / "$ TMPDIR" /system.tar etc var / spool / cron / crontabs
 # We pack in bzip-archive
 tar -cjf backup - "$ TIMEY" .tbz2 "$ TMPDIR"
 mv backup - "$ TIMEY" .tbz2 "$ YADISK"
 # Delete temporary files
 rm -r "$ TMPDIR"
 # Delete old archives
 find "$ YADISK" -type f -mtime +4 |  xargs rm -f
 # Disable Yandex.Disk
 umount "$ YADISK"
 exit 1 

It runs twice a day on the crown. Creates copies of databases, certain system settings, as well as user data (sites). Partially, it consists of a script for copying only databases published previously.

Sometimes an error may appear in syslog:

  mount.davfs: open files exceed max cache size by 50 MiBytes 

In this case, we mount the Yandex.Disk section and see what lies in the lost + found folder. We clean it. Then we clean the contents of the / var / cache / davfs2 / folder.

How do you rate the article?
Звёзд: 1Звёзд: 2Звёзд: 3Звёзд: 4Звёзд: 5 (No ratings yet)

Add a comment

Your email will not be published.