I am looking for a way to backup/sync the contents of a CIFS share to an external USB in Ubuntu, from the CLI. I have mounted the cifs share and also mounted the external storage USB.

I have the following directory structure on the CIFS share:

$ mount -t cifs -o
    //<mycifs_ip_address>/<mycifs_share_name> /mnt/myshare
$ ls /mnt/myshare

BackupFiles > 
            > 11_28_2014
                        > File_Today_11_16_2013.txt
                        > File_Yesterday_07_02_2014.txt
            > 07_16_2015
                        > File_Yesterday_04_29_2015.txt
                        > File_Tomorrow_10_12_2015.txt
                        > File_Yesterday_07_02_2015.txt
            > 12_05_2016
                        > File_Tomorrow_06_10_2016.txt

Note that 11_28_2014, 07_16_2015 and 12_05_2016 are directories.

I have the following directory structure on the external USB:

$ mount /dev/sha4 mnt/myusbback
$ ls /mnt/myusbback

BackupFiles >
            > 11_28_2014
                        > File_Today_11_16_2013.txt
                        > File_Yesterday_07_02_2014.txt

Additional information:

  1. I am using Ubuntu Server 16.04.1
  2. When mounting the CIFS share, I need to specify a username and password. The CIFS share has been set up to require these credentials when accessing it.


I am looking for a way to:

  1. sense the directory structure on the CIFS share and on the external USB and create missing directories on the USB
  2. copy files from the CIFS share into appropriate folders on the USB, if they are missing from the USB
  3. Sync the BackupFiles directory (including all its sub-directories) on the CIFS share and the BackupFiles directory on the external usb storage.

In summary, I think I need a way to sync all files (including the directory and sub-directory structure) from the CIFS share onto the external USB storage.


Is there a way to do this in Ubuntu from the CLI? Will I be prompted to specify the CIFS share username and password?

  • 1
    sounds like a task for rsync, you could use a .credentials file when mounting the share, if zou dont want to have to type in the credentials. – Bruni Dec 4 '16 at 18:04
  • Ok, so on my local machine I tried rsync this way: rsync -rlDtzuv myusername@ip:/mnt/..... /mnt/usb/. The USB storage is connected and mounted to the local machine. I do NOT need to mount the remote CIFS storage. It works as I required. Thanks for your suggestion. However, I am having to manually enter the password for the CIFS share user when this runs. I am running this command from the local machine so I am not SSH'ing. I imagine I have to set up some acess keys. Which key (public or private) do I place on the local machine and which key do I place on the CIFS share? – edesz Dec 4 '16 at 20:09
  • Also, where I do generate the keypair - on the local machine or on the remote machine? – edesz Dec 4 '16 at 20:10
  • If you are running on the local network, without ssh, my understanding is that you don't need to setup a key pair. I personally use rsync with mounted directories 'mount -t cifs -o credentials=/root/.credentials,uid=bruni,gid=users //xx.xxx.xxx.xxx/Share /mountpoint ', were the credentials are in .credentials and then 'rsync -rutv'. But there should be a lot of rsync gurus around to jump in and help. – Bruni Dec 5 '16 at 7:18
  • Ah, I see. Yeah, as I mentioned I am not mounting the CIFS share and rsync works as expected. I would prefer to use this approach for my application. However, I still don't know where the place the appropriate keys. – edesz Dec 5 '16 at 19:58

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.