# RSync

RSync copies files and can sync between servers

# Backing up

RSync copies files and can sync between servers

Use RSync in \[\[Cronjob\]\]  
Archive Backups with \[\[tar\]\]

TEST WITH: ``--dry-run``

```yaml
rsync -aAXv --delete --exclude={/home/danicus/.cache/*} /home /mounted/backup
```

 a= archive= its a few other tags as well  
 A= keeps permissions  
 X= keeps the next level of permissions  
 v= verbose  
\-- delete= deletes any files inside the file this will be copying to  
\--exclude= excludes `file/this/\*, andfile/that/\*`  
 /home= location of files you want to back up  
 /mounted= the backup drive location  
 see \[\[RSync#\*NOTE "/"\*\]\]  
  
\#### Multiple directories to one end location  
\###### use seperate command lines and run them seperately.

```yaml
sudo rsync -aAXv --delete /etc /mounted/backup/wallserver/
sudo rsync -aAXv --delete /home /mounted/backup/wallserver/
sudo rsync -aAXv --delete /root /mounted/backup/wallserver/
sudo rsync -aAXv --delete /var /mounted/backup/wallserver/
sudo rsync -aAXv --delete /usr/local/bin /mounted/backup/wallserver/
sudo rsync -aAXv --delete /usr/local/sbin /mounted/backup/wallserver/
sudo rsync -aAXv --delete /srv /mounted/backup/wallserver/
sudo rsync -aAXv --delete /opt /mounted/backup/wallserver/
sudo rsync -aAXv --delete /mounted/main /mounted/backup/wallserver/
```

<div id="bkmrk-%23%23%23%23-multiple-direct">\#### Multiple directories to one end location FROM an Remote Source</div><div id="bkmrk-rsync--e-ssh-root%40re">rsync -e ssh root@remote.com:/path/to/file :/path/to/second/file/ /local/directory/</div><div id="bkmrk-">  
</div><div id="bkmrk--0">  
</div><div id="bkmrk-%3E%23%23%23%23-permissions-de">&gt;#### Permissions denied (this my not work)</div><div id="bkmrk-if-you-are-denied-be">If you are denied because of `PermitRootLogin no`, create a new user</div><div id="bkmrk-%3E">&gt;</div><div id="bkmrk-%3Emake-a-new-user-for">&gt;Make a new user for rsync to use then add that name to the /etc/sudoers &gt;file so that, that user can execute the rsync command without a &gt;password</div><div id="bkmrk-%3Ein-the-%2Fetc%2Fsudoers">&gt;In the /etc/sudoers file add:</div><div id="bkmrk-%3E%60rsyncuser-all%3D-nop">&gt;`rsyncuser ALL= NOPASSWD:/usr/bin/rsync`</div><div id="bkmrk-%3E-0">&gt;</div><div id="bkmrk-%3Ethen%2C-we-use-_%2A%2Arsy">&gt;Then, we use _**rsync-path**_ to make _rsync_ works with the sudo.</div><div id="bkmrk-%3E%60--rsync-path%3D%22sudo">&gt;`--rsync-path="sudo rsync"`</div><div id="bkmrk-%3E%60rsync--aaxv---rsyn">&gt;`rsync -aAXv --rsync-path="sudo rsync" --delete`</div>

# How to restore backup with RSync

[https://askubuntu.com/questions/1011136/how-to-restore-backup-with-rsync](https://askubuntu.com/questions/1011136/how-to-restore-backup-with-rsync)

asically just revierse the order for the backup.   
-tag it `rsync -aAXv`  
Then start from backup directory and end with destination directory

```bash
sudo rsync -aAXv --delete /mounted/backups/cpbackups/ /mounted/main/directory/location/
```

+from Backtup= /mounted/backups/cpbackups   
+To Destination= /mounted/main/directory/location?

\*NOTE "/"\*  
The "/" means something!  
At the end of the directory, if you do not include the "/" Rsync will make a directory inside the directory you specify, then will include the files in from there.  
If you DO include the "/", it will drop the files/directories in, directly at the directory that you specify.

Example:

```bash
sudo rsync -aAXv --delete
```

   
((1)) /mounted/backups/cpbackups  
((2)) /mounted/main/directory/location?

Without "/"  
((1)) File name will be "cpbackups" and will make a directory named "cpbackups" and put it in ((2)).

With "/"  
Will take files from INSIDE ((1)) and put them INSIDE the ((2)) directory.

\*\*Change the flags\*\*

```bash
rsync -aHAXvP /home_folder_path /backup_folder_path
```