File backup automation using cron and rsync

Learn how to automate file backups using cron and rsync.

Create a backup directory on the backup server

Create a backup directory on your backup server. Suppose "/ datadrive" already exists.

mkdir -p / datadrive / backup

Use rsync for file backup

Use rsync to back up your files.

sudo rsync -e "ssh -p22" -av --delete /datadrive/myapp@133.242.230.50:/datadrive/backup/datadrive/
sudo rsync -e "ssh -p22" -av --delete /etc_letsencrypt/ myapp@133.242.230.50:/datadrive/backup/etc_letsencrypt/

rsync can copy directories recursively and only copy the diffs of the files, so the only time to update the files is to check the diffs and copy the diffs.

If you specify the "--delete" option, deleted files will also be detected and mirroring will be possible.

Transfer files from the production machine to the backup machine

The file is transferred from the production machine to the backup machine. For security reasons, root login to the production machine seems to be prohibited by SSH.

Some files you want to back up require root privileges.

Therefore, execute the rsync command on the production machine side to transfer it to the backup machine.

Create a private / public key pair and register the public key on the backup server

On the production machine, use the ssh-keygen command to create a private / public key pair and register the public key on the backup server.

Generate a private / public key pair in the root directory.

sudo mkdir -p /root/.ssh
sudo chmod 700 /root/.ssh

# File name is "/root/.ssh/id_rsa_backup"
sudo ssh-keygen -t rsa

Shows the public key.

sudo cat /root/.ssh/id_rsa_backup.pub

Make a copy of the displayed public key.

Work on a backup server.

mkdir -p ~ / .ssh
chmod 700 ~ / .ssh
cd ~ / .ssh

Register the public key. Open the "authorized_keys" file and paste the copied public key at the bottom.

vi ~ / .ssh / authorized_keys

Now you can do rsync with SSH public key authentication.

sudo rsync -e "ssh -p22 -i /root/.ssh/id_rsa_backup" -av --delete /datadrive/myapp@133.242.230.50:/datadrive/backup/datadrive/
sudo rsync -e "ssh -p22 -i /root/.ssh/id_rsa_backup" -av --delete /etc_letsencrypt/ myapp@133.242.230.50:/datadrive/backup/etc_letsencrypt/

Create a backup directory as well.

Register file backup in root crontab

Register the command to be executed in crontab of the root of the production machine.

You can open the root crontab as follows:

sudo crontab -e

Write the backup command in crontab as follows. Please note that sudo is not written in crontab.

Time is arbitrary. I think it's better to have less processing at night.

MAIL is the email address you will be notified if rsync fails. Remove the "-v" option to show details.

MAILTO = kimoto.yuki@gmail.com

30 03 * * * rsync -e "ssh -p22 -i /root/.ssh/id_rsa_backup" -a --delete /datadrive/myapp@133.242.230.50:/datadrive/backup/datadrive/
30 03 * * * rsync -e "ssh -p22 -i /root/.ssh/id_rsa_backup" -a --delete /etc_letsencrypt/ myapp@133.242.230.50:/datadrive/backup/etc_letsencrypt/

Note that the rsync command can't create multi-level directories like "mkdir -p".

Associated Information