It is good to have a backup of your server. After you configure your server and run a website don’t forget prepare automatic backup. I also encourage you to do the same with your data at home.
You can also make a manual backup which is not bad, however I know from experience that sooner or later you will forget, or give up this manual task. In fact, the whole process can be completely automated. That is why it is worth to configure it once and only monitor it from time to time.
Just after setting up the server and its configuration you should make a copy of all configuration files in which you made some changes. It is enough to make such copy once and update only when some changes are made. As the changes in the configuration do not take place too often, you can do this part manually.
The most important is the backup of services that work on your server. For example, I will describe below the backup of two most popular services on the server. Website server and database.
To make everything work properly you need a few additional packages, install them with the commands below.
Rclone - is a command line program to sync files and directories.
curl https://rclone.org/install.sh | sudo bash
7zip - is a file archiver with a high compression ratio.
sudo apt install p7zip-full
To make a MySQL/MariaDB database backup and compress it use command:
mysqldump -u root --password='database_password' database_name | gzip > /home/user/backup/database_name_db_$(date +\%Y_\%m_\%d).sql.gz
Update parameters with your
database name and
location of backup.
To make website files backup with compression use command:
/usr/bin/7z a -t7z -mx=3 /home/user/backup/backup_$(date +\%Y-\%m-\%d).7z /var/www/website_files/
Also update parameters like compression ratio
backup location and
path to website files.
It is good to create backup script rather than add above commands to crontab separately.
So create and edit new script file:
add your backup commands:
allow script to be executed:
chmod +x ./backup.sh
In this script we will use rclone to send backup files to remote location.
rclone copy /home/user/backup/ remote:backup
Just run command:
sudo rclone config
and follow configuration wizard.
Rclone configuration is located in user folder
/home/user/.config/rclone/rclone.conf. If you will configure rclone as user and run cron as root then you should add path to configuration file as parameter.
rclone --config /home/user/.config/rclone/rclone.conf copy /home/user/backup/ remote:backup
or just configure rclone as root.
It’s important to keep backups in a different location than on a server from which we make copies ;)
If you don’t want to use database password in script file you can configure username and password in root folder. Create mysqldump config file as root.
and add there your database info:
chmod 600 ~/.my.cnf
mysqldump commands takes the password from this file. Then your backup script can look like this:
Once your remote location is configured schedule backup script execution using crontab
sudo crontab -e
0 0 * * 0 /bin/bash /home/user/backup.sh > /dev/null 2>&1
This will execute backup script at 00:00 on Sunday. To adjust your own time you can use crontab guru.
/dev/null will notify when error occurs
/dev/null 2>&1 shows nothing
If you want check cron logs use command:
sudo grep CRON /var/log/syslog
That’s all! Backup your stuff and be happy :)