HOW TO: Automatically backup your user data with rsync

Here is a quick and easy way to backup your files and MySQL database with a simple script:

 

  1. Create an SSH RSA Key by logging into your VPS and using the command ssh-keygen. We've used rsynckey as our filename and no passphrase.
  2. Create a file on your server that you will be using as your backup script. In this example we will create a file called backup.sh and placing it in /home/user/.
  3. In the file place the following code and edit it with your details (more information below):
    mysqldump -hLOCALHOST -uDBUSERNAME -pDBPASSWORD DATABASE | gzip > /home/user/backup/DATABASE.sql.gz
    tar -zcf /home/user/backup/userhome.tar.gz /home/user --exclude "/home/user/backup"
    rsync -av --delete -e "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /home/USERNAME/.ssh/rsynckey" /home/user/backup/ USER@my.vps.com:backup
  4. Make the script executable (chmod 0755 backup.sh).
  5. Create a cron job either by editing crontab manually (crontab -e) or in your control panel (cPanel, DirectAdmin, Kloxo, etc...) and set the interval to whatever you like (we recommend at least daily, but we do one every 4 hours for more current backups) for the command simply put sh /home/user/backup.sh and save.
  6. Wait for the backup to run and confirm there are no errors.

A breakdown of the above script:
mysqldump -hLOCALHOST -uDBUSERNAME -pDBPASSWORD DATABASE | gzip > /home/user/backup/DATABASE.sql.gz
This command will create a backup of your MySQL database in your backup directory. For more specifics on how to edit this line, please visit this link.
If you have more databases you wish to backup you can use this line as a template and add as many as you'd like to this script.

tar -zcf /home/user/backup/userhome.tar.gz /home/user --exclude "/home/user/backup"
This command will tar and compress your whole user directory except for your backup directory and save it to your backup directory.
If you have more folders or files you wish to backup you can use this line as a template and add as many as you'd like to this script.

rsync -av --delete -e "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /home/user/.ssh/rsynckey" /home/user/backup/ user@my.vps.com:backup
This will rsync your backup directory over to the Secure Dragon server using your SSH RSA Key. If you set a passphrase for your SSH RSA Key then you will not be able to automate this task.
If you would like to backup your data to multiple locations you can use this line as a template and add as many as you'd like to this script.

 

  • 2 Utilisateurs l'ont trouvée utile
Cette réponse était-elle pertinente?

Articles connexes

HOW TO: Backing up files with 1 line of code

rsync -avz /my/folder/ user@SERVERIP:/home/user/my/folderYup, that's it. All you do is enter the...

HOW TO: Cancel your service

Login to the Client Area. Click on "My Services" under the "Services" tab at the top. Click...

HOW TO: Creating an SSH RSA Key with PuTTYgen.exe

1) Open PuTTYgen.exe set the type to SSH2 RSA and minimum 1024 bits.2) Click "Generate" and move...

HOW TO: Backup your MySQL database

The easiest way to automatically backup your database is with a simple cron job running the...

HOW TO: Setup a very basic Debian VNC server (XFCE4 Desktop)

For those of you who are interested in adding a GUI to your server that you can connect remotely...