LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Need help with a Nightly backup to a usb device (https://www.linuxquestions.org/questions/linux-newbie-8/need-help-with-a-nightly-backup-to-a-usb-device-857613/)

beeson76 01-20-2011 02:07 PM

Need help with a Nightly backup to a usb device
 
Hello.

I need some answers to some very simple (hopefully not complex) questions I have.

I have Mandriva 2006 on a computer that I regularly backup important directories every night to a flash drive. I use Cron for my backup with scheduled backups nightly. For my script file to do the backup, there is a line that says to copy these directories to /media/sda/backup, so I figure it was copying to the sda named usb device. Well it wasn't. It is copying to the /media/sda/backup directory on my harddrive. But I would like to copy it to a usb device. How do I find out the name of my usb device that I have plugged in? And when writing the script for it would it be under the /dev directory? Thanks.

Dark_Helmet 01-20-2011 03:38 PM

It's not complicated, but I have to ask some questions. Is the flash drive always connected to the computer? Do you swap it out with a different one each night?

Are you familiar with mounting and unmounting filesystems?

tredegar 01-20-2011 03:53 PM

The above advice is basically good, but Mandriva 2006 is way out of date, and you should probably not be using it at all.

Please update your linux distro, because a lot has changed for the better over the last five years. You might be pleasantly surprised.

beeson76 01-20-2011 04:06 PM

Thanks Dark Helmet for the reply. I think I will be switching out usb devices every once in a while for the backup. I am not really too familiar with the mounting and unmounting. If you would like to tell me more or a simple website would be nice. I certainly appreciate the help.

FredGSanford 01-20-2011 06:31 PM

Have you looked into the Control Center, Snapshot Backup Configuration tool?

As mention, it may be best to update to the latest version of Mandriva 2010.2, if possible.

silvyus_06 01-20-2011 06:41 PM

errrm .. let me try to add something to actually give you a solution and not tell you what is better to do... (which is mainly a big problem on the linux forums)

make another cronjob script with right a few minutes before the actual backup script starts . for example , if you have only one harddrive on this computer , the usb drive *should* be /dev/sdb1(the first partition on device b,change these with your number and letters)

and if that script fails(e.g. no usb drive in the computer) put in the backup script some lines to check if /dev/sdb is mounted and if it is mounted to continue, but if not to abort the backup.

hopefully posting the script here would help the other users give you examples of the updated script .

good luck

jlinkels 01-20-2011 07:16 PM

This is the script I use to be sure things are backed up on the external USB drive instead of the mount directory of my hard disk.

Code:

#!/bin/bash

mount_point='/mnt/ext_daily'
echo_flag=''

# Find if the device is mounted

df -h | grep $mount_point > /dev/null
if [ $? -eq 0 ]
then
        $echo_flag rsync -ua --exclude='/mnt' --exclude='/proc' --exclude='/sys' --exclude='/vmware' / /mnt/ext_daily > /var/log/rsync_daily
        echo "mount point $mount_point exists, rsync started"
else
        echo "Error: mount point $mount_point does not exist, rsync operation skipped"
fi

What I do is ask a df and see if the mounted drive is listed. If it is not mounted it is not listed and grep returns 1. I call this script from CRON, an email is sent with the outcome of the test.

jlinkels

Larry Webb 01-20-2011 08:49 PM

If you want to find the /dev/sdxx, if it will auto mount the easiest way I know is first unplugged do from terminal

cat /proc/partitions

and then plug in your usb then run again and what ever shows up different should be your drive.

If that does not work then do

fdisk -l (small L) as root with the same as above unplugged then again plugged. If it does not show in the first but does in the second means it is not mounted automatically and you will need to add it to your fstab or manually mount each time you plug.

Dark_Helmet 01-20-2011 10:18 PM

Choosing whether to upgrade Mandriva or not is up to you. I'll assume that if you don't upgrade, there's a reason for it (older hardware, too much of a PITA, can't afford the downtime, etc.). What I planned to suggest works regardless.

Mounting and unmounting is pretty simple. When you plug a device into the machine, if all goes well, the kernel creates a device file. The device file is located in /dev and for a USB storage device (thumb drive, external HD, whatever), it's usually named sdX? (where X is a letter from a-z and ? is a number starting from 1). So, an example would be "/dev/sdc1."

For what you want to do, you cannot read/write files directly to the device file. You must mount it first. Mounting a device just integrates it into the filesystem. What you were doing originally would have worked as you expected had your USB device been mounted at /media/sda.

Enough talk... a practical example. There are other ways of doing this (see jlinkels' reply using df and Larry Webb's reply using /proc/partitions). Open a terminal, command line, shell, or whatever you like to call it. Plug in your USB drive. Now type:
Code:

dmesg
You'll get a whole bunch of info, but you're only interested in a small portion--should be the last bit. I just plugged in one of my own, and this is the tail end of what I got:
Code:

[964443.750096] usb 1-1: new high speed USB device using ehci_hcd and address 26
[964443.905301] usb 1-1: configuration #1 chosen from 1 choice
[964443.906204] scsi23 : SCSI emulation for USB Mass Storage devices
[964443.906400] usb-storage: device found at 26
[964443.906405] usb-storage: waiting for device to settle before scanning
[964448.900376] usb-storage: device scan complete
[964448.900974] scsi 23:0:0:0: Direct-Access    LEXAR    JUMPDRIVE ELITE  2000 PQ: 0 ANSI: 0 CCS
[964448.901929] sd 23:0:0:0: Attached scsi generic sg6 type 0
[964448.904498] sd 23:0:0:0: [sdg] 502880 512-byte logical blocks: (257 MB/245 MiB)
[964448.905299] sd 23:0:0:0: [sdg] Write Protect is off
[964448.905307] sd 23:0:0:0: [sdg] Mode Sense: 43 00 00 00
[964448.905313] sd 23:0:0:0: [sdg] Assuming drive cache: write through
[964448.908563] sd 23:0:0:0: [sdg] Assuming drive cache: write through
[964448.908675]  sdg: sdg1
[964448.960796] sd 23:0:0:0: [sdg] Assuming drive cache: write through
[964448.960907] sd 23:0:0:0: [sdg] Attached SCSI removable disk

The important stuff is highlighted in red. For me, the USB thumbdrive has been assigned two device files: /dev/sdg and /dev/sdg1. We're not concerned with the /dev/sdg device file--only /dev/sdg1 (the number represents a partition on the device--generally only one partition on a thumb drive). So now you mount it:
Code:

sudo mount -o uid=xxx,gid=yyy /dev/sdg1 /media/usb_drive
A couple things:
(1) You will need root privileges to execute mount. On Ubuntu systems, we use sudo. I'm not familiar with Mandriva. You may need to execute "su" first and enter the root password--then execute the mount command without the sudo at the beginning.

(2) Replace the "xxx" and "yyy" parts of the command with your text username and text groupname. For example, if your username is beeson76, do something like "uid=beeson76,gid=beeson76". Watch the spacing and if you don't know your default group, you can probably leave off the gid portion altogether. This option will allow your regular account to save your backup to the drive--no special permissions needed.

(3) Replace "/media/usb_drive" with whatever directory you want. It can be anything so long as the directory exists. After the mount is successful, writes to that directory will be diverted to your USB drive. Then update your command/script so that the backup is written to the same directory that you mounted the USB drive to.

When you want to remove the USB thumbdrive, execute:
Code:

sudo umount /dev/sdg1
Again, the sudo is only to get root privileges. Replace that with whatever Mandriva does by default.

Now, with all that said, there are some things that can automate the process a little bit (e.g. writing one or two udev rules to automatically mount one or more USB thumb drives to the appropriate location). If you're interested, either I or some other folks here can help you with that later. I figure this is enough to chew on at the moment.


All times are GMT -5. The time now is 06:12 PM.